我有张量流的CNN网络 . 我正在保存模型并尝试恢复 . 我读了很多关于保存和恢复模型的建议 . 我能够为前馈网络做到这一点,但CNN证明有点困难 . 以下是代码的一些片段
1. Import the Data (External Source)
2. Main CNN code
def conv_net(a):
with tf.variable_scope("conv_cell", reuse=tf.AUTO_REUSE):
conv11 = tf.layers.conv1d(a, 50, 2, activation=tf.nn.relu, name='conv11')
conv1 = tf.layers.max_pooling1d(conv11, 2, 1, name='conv1'
conv21 = tf.layers.conv1d(conv1, 30, 2, activation=tf.nn.relu, name='conv21')
conv2 = tf.layers.max_pooling1d(conv21, 2, 1, name='conv2')
fc11 = tf.contrib.layers.flatten(conv2)
fc1 = tf.layers.dense(fc11, 180, name='conv3')
outputs1 = tf.layers.dense(fc1, timesteps/10, name='conv4')
outputs = tf.reshape(outputs1, [-1, 1])
return outputs
Y_Pred1 = conv_net(X)
Y_Pred = tf.reshape(Y_Pred1, [-1, TimeWindow, num_input])
Loss = tf.reduce_sum((Y - Y_Pred)**2)
optimizer = tf.train.AdamOptimizer(Learning_rate).minimize(Loss)
3. Main Session
with tf.Session() as sess:
sess.run(init)
saver = tf.train.Saver()
------------------------------------------(训练)----- -------------------------------------------
save_path = saver.save(sess, './Save_Network/my_model')
print("Model saved in file: %s" % save_path)
4. My Question
我不确定,它节省了什么 . 因为我没有单独定义重量和偏差 . 重量和偏差由内置命令“tf.layers.conv1d”内部定义 . 我不知道如何恢复它们 . 我尝试了以下恢复,并按预期不起作用 .
with tf.Session() as sess:
new_saver = tf.train.import_meta_graph('./Save_Network/my_model.meta')
new_saver.restore(sess, tf.train.latest_checkpoint('./Save_Network'))
def conv_net(a):
conv11 = tf.layers.conv1d(a, 50, 2, activation=tf.nn.relu)
conv1 = tf.layers.max_pooling1d(conv11, 2, 1)
conv21 = tf.layers.conv1d(conv1, 30, 2, activation=tf.nn.relu)
conv2 = tf.layers.max_pooling1d(conv21, 2, 1)
fc11 = tf.contrib.layers.flatten(conv2)
fc1 = tf.layers.dense(fc11, 180)
outputs1 = tf.layers.dense(fc1, timesteps/10)
outputs = tf.reshape(outputs1, [-1, 1])
return outputs
Y_Pred1 = conv_net(X)
Y_Pred = tf.reshape(Y_Pred1, [-1, TimeWindow, num_input])
4. Error I get FailedPreconditionError(参见上面的回溯):尝试使用未初始化的值conv1d / kernel [[Node:conv1d / kernel / read = IdentityT = DT_FLOAT,_device = "/job:localhost/replica:0/task:0/device:CPU:0"]]
- 我尝试使用init = tf.global_variables_initializer()sess.run(init)初始化变量
那没用 .
我如何能够访问这些权重和偏见并恢复它们 .