【发布时间】:2020-04-14 19:00:29
【问题描述】:
我对 phyton 编程非常陌生。我在训练时保存了 tensorflow 神经网络:
n_hidden_1 =200
n_hidden_2 =100 # 1st layer num features
n_hidden_3 = 100 # 2nd layer num features
n_input = 128 n_output = 128
X = tf.placeholder("float", [None, n_input])
Y = tf.placeholder("float", [None, n_output])
def encoder(x):
weights = {
'encoder_w1': tf.Variable(tf.random.truncated_normal([n_input, n_hidden_1],stddev=0.1),name='encoder_w1'),
'encoder_w2': tf.Variable(tf.random.truncated_normal([n_hidden_1, n_hidden_2],stddev=0.1),name='encoder_w2'),
'encoder_w3': tf.Variable(tf.random.truncated_normal([n_hidden_2, n_hidden_3],stddev=0.1),name='encoder_w3'),
'encoder_w4': tf.Variable(tf.random.truncated_normal([n_hidden_3, n_output],stddev=0.1),name='encoder_w4'),
}
biases = {
'encoder_b1': tf.Variable(tf.random.truncated_normal([n_hidden_1],stddev=0.1),name='encoder_b1'),
'encoder_b2': tf.Variable(tf.random.truncated_normal([n_hidden_2],stddev=0.1),name='encoder_b2'),
'encoder_b3': tf.Variable(tf.random.truncated_normal([n_hidden_3],stddev=0.1),name='encoder_b3'),
'encoder_b4': tf.Variable(tf.random.truncated_normal([n_output],stddev=0.1),name='encoder_b4'),
}
# Encoder Hidden layer with sigmoid activation #1
#layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['encoder_h1']), biases['encoder_b1']))
layer_1 = tf.nn.relu(tf.add(tf.matmul(x, weights['encoder_w1']), biases['encoder_b1']))
layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1, weights['encoder_w2']), biases['encoder_b2']))
layer_3 = tf.nn.relu(tf.add(tf.matmul(layer_2, weights['encoder_w3']), biases['encoder_b3']))
layer_4 = tf.nn.sigmoid(tf.add(tf.matmul(layer_3, weights['encoder_w4']), biases['encoder_b4']))
return layer_4
y_pred = encoder(X)
y_true = Y
# Define loss and optimizer, minimize the squared error
cost = tf.reduce_mean(tf.pow(y_true - y_pred, 2))
learning_rate = tf.placeholder(tf.float32, shape=[])
optimizer = tf.train.RMSPropOptimizer(learning_rate=learning_rate).minimize(cost)
saver = tf.train.Saver(max_to_keep=1)
with tf.Session(config=config) as sess:
sess.run(init)
traing_epochs = 100
learning_rate_current = 0.001 #0.01
for epoch in range(traing_epochs):
> # Code Block.................
input_labels.append(x)
input_samples.append(y)
batch_x = np.asarray(input_samples)
batch_y = np.asarray(input_labels
_,c = sess.run([optimizer,cost], feed_dict={X:batch_x, Y:batch_y, learning_rate:learning_rate_current})
训练后,我将训练后的网络打印变量加载为:
with tf.Session() as sess2:
saver2= tf.train.import_meta_graph('../my_model.ckpt.meta')
saver2.restore(sess2,'../my_model.ckpt')
savevariable=tf.all_variables()
我在训练后看到保存的变量,存在 48 个变量(为什么 8 个初始参数增加到 48 个值我不明白)我有 4 个权重和 4 个偏差用于第 1 层、第 2 层和第 3 层以及第 4 层的初始值.为了预测我应该从这个保存的值中使用哪个 4 个权重和 4 个偏差?我很困惑。在此处打印保存变量:
<tf.Variable 'encoder_w1:0' shape=(128, 200) dtype=float32_ref>,
<tf.Variable 'encoder_w2:0' shape=(200, 100) dtype=float32_ref>,
<tf.Variable 'encoder_w3:0' shape=(100, 100) dtype=float32_ref>,
<tf.Variable 'encoder_w4:0' shape=(100, 128) dtype=float32_ref>,
<tf.Variable 'encoder_b1:0' shape=(200,) dtype=float32_ref>,
<tf.Variable 'encoder_b2:0' shape=(100,) dtype=float32_ref>,
<tf.Variable 'encoder_b3:0' shape=(100,) dtype=float32_ref>,
<tf.Variable 'encoder_b4:0' shape=(128,) dtype=float32_ref>,
<tf.Variable 'encoder_w1/RMSProp:0' shape=(128, 200) dtype=float32_ref>,
<tf.Variable 'encoder_w1/RMSProp_1:0' shape=(128, 200) dtype=float32_ref>,
<tf.Variable 'encoder_w2/RMSProp:0' shape=(200, 100) dtype=float32_ref>,
<tf.Variable 'encoder_w2/RMSProp_1:0' shape=(200, 100) dtype=float32_ref>,
<tf.Variable 'encoder_w3/RMSProp:0' shape=(100, 100) dtype=float32_ref>,
<tf.Variable 'encoder_w3/RMSProp_1:0' shape=(100, 100) dtype=float32_ref>,
<tf.Variable 'encoder_w4/RMSProp:0' shape=(100, 128) dtype=float32_ref>,
<tf.Variable 'encoder_w4/RMSProp_1:0' shape=(100, 128) dtype=float32_ref>,
<tf.Variable 'encoder_b1/RMSProp:0' shape=(200,) dtype=float32_ref>,
<tf.Variable 'encoder_b1/RMSProp_1:0' shape=(200,) dtype=float32_ref>,
<tf.Variable 'encoder_b2/RMSProp:0' shape=(100,) dtype=float32_ref>,
<tf.Variable 'encoder_b2/RMSProp_1:0' shape=(100,) dtype=float32_ref>,
<tf.Variable 'encoder_b3/RMSProp:0' shape=(100,) dtype=float32_ref>,
<tf.Variable 'encoder_b3/RMSProp_1:0' shape=(100,) dtype=float32_ref>,
<tf.Variable 'encoder_b4/RMSProp:0' shape=(128,) dtype=float32_ref>,
<tf.Variable 'encoder_b4/RMSProp_1:0' shape=(128,) dtype=float32_ref>,
<tf.Variable 'encoder_w1:0' shape=(128, 200) dtype=float32_ref>,
<tf.Variable 'encoder_w2:0' shape=(200, 100) dtype=float32_ref>,
<tf.Variable 'encoder_w3:0' shape=(100, 100) dtype=float32_ref>,
<tf.Variable 'encoder_w4:0' shape=(100, 128) dtype=float32_ref>,
<tf.Variable 'encoder_b1:0' shape=(200,) dtype=float32_ref>,
<tf.Variable 'encoder_b2:0' shape=(100,) dtype=float32_ref>,
<tf.Variable 'encoder_b3:0' shape=(100,) dtype=float32_ref>,
<tf.Variable 'encoder_b4:0' shape=(128,) dtype=float32_ref>,
<tf.Variable 'encoder_w1/RMSProp:0' shape=(128, 200) dtype=float32_ref>,
<tf.Variable 'encoder_w1/RMSProp_1:0' shape=(128, 200) dtype=float32_ref>,
<tf.Variable 'encoder_w2/RMSProp:0' shape=(200, 100) dtype=float32_ref>,
<tf.Variable 'encoder_w2/RMSProp_1:0' shape=(200, 100) dtype=float32_ref>,
<tf.Variable 'encoder_w3/RMSProp:0' shape=(100, 100) dtype=float32_ref>,
<tf.Variable 'encoder_w3/RMSProp_1:0' shape=(100, 100) dtype=float32_ref>,
<tf.Variable 'encoder_w4/RMSProp:0' shape=(100, 128) dtype=float32_ref>,
<tf.Variable 'encoder_w4/RMSProp_1:0' shape=(100, 128) dtype=float32_ref>,
<tf.Variable 'encoder_b1/RMSProp:0' shape=(200,) dtype=float32_ref>,
<tf.Variable 'encoder_b1/RMSProp_1:0' shape=(200,) dtype=float32_ref>,
<tf.Variable 'encoder_b2/RMSProp:0' shape=(100,) dtype=float32_ref>,
<tf.Variable 'encoder_b2/RMSProp_1:0' shape=(100,) dtype=float32_ref>,
<tf.Variable 'encoder_b3/RMSProp:0' shape=(100,) dtype=float32_ref>,
<tf.Variable 'encoder_b3/RMSProp_1:0' shape=(100,) dtype=float32_ref>,
<tf.Variable 'encoder_b4/RMSProp:0' shape=(128,) dtype=float32_ref>,
<tf.Variable 'encoder_b4/RMSProp_1:0' shape=(128,) dtype=float32_ref>
你能给我建议吗?
【问题讨论】:
-
您的问题必须是可重现的,所以请提供一些简单的工作代码和示例数据,您接受过培训。
标签: python tensorflow deep-learning artificial-intelligence