其他分享
首页 > 其他分享> > TensorFlow实践笔记

TensorFlow实践笔记

作者:互联网

TensorFlow

安装

python3 -m pip install --upgrade tensorflow

keras API

Functional API

input_A = keras.layers.Input(shape=[5], name="wide_input")
hidden1 = keras.layers.Dense(30, activation="relu")(input_B)
concat = keras.layers.concatenate([input_A, hidden2])
output = keras.layers.Dense(1, name="main_output")(concat)
aux_output = keras.layers.Dense(1, name="aux_output")(hidden2)
model = keras.Model(inputs=[input_A, input_B], outputs=[output, aux_output])
model.summary()
model.compile(loss=["mse", "mse"], loss_weights=[0.9, 0.1], optimizer="sgd")
history = model.fit([X_train_A, X_train_B], [y_train, y_train], epochs=20,validation_data=([X_valid_A, X_valid_B], [y_valid, y_valid]))#valid验证集
model.evaluate(X_test, y_test)
y_proba = model.predict(X_new)
model.save("my_keras_model.h5")
model = keras.models.load_model("my_keras_model.h5")
checkpoint_cb = keras.callbacks.ModelCheckpoint("my_keras_model.h5",
						save_best_only=True)#save_best_only只保存验证集最好的模型,若无则默认每个周期保存
						early_stopping_cb = keras.callbacks.EarlyStopping(patience=10,
                              restore_best_weights=True)#早停
history = model.fit(X_train, y_train, epochs=100,
                    validation_data=(X_valid, y_valid),
                    callbacks=[checkpoint_cb, early_stopping_cb])#调回

其他keras.callbacks

Subclassing API

操作

tensorboard_cb = keras.callbacks.TensorBoard(run_logdir)#创建日志详见chapter10
fit的时候callbacks=[tensorboard_cb]

在日志目录下运行tensorboard打开浏览器
其他信息:选项、tf.summary包

数据结构

张量

tf.Tensor值不能修改

t=tf.constant([[1., 2., 3.], [4., 5., 6.]]) 

索引和一些基本运算与numpy类似(具体见chapter11
@等于tf.matmul()
NumPy默认使用64位精度,TensorFlow默认用32位精度,当你用NumPy数组创建张量时,一定要设置dtype=tf.float32

变量

tf.Variable()
修改值v.assign(2)

标签:keras,实践,笔记,callbacks,train,tf,TensorFlow,model,valid
来源: https://blog.csdn.net/qq_38591130/article/details/105891306