tensorflow - 从零开始在 Tensorflow 中连接连体神经网络:形状不匹配
问题描述
我正在使用以下代码在 Tensorflow 中开发连体神经网络:
X = tf.placeholder(dtype=tf.float32)
X2 = tf.placeholder(dtype=tf.float32)
Y = tf.placeholder(dtype=tf.float32)
num_hidden=128
# Build a hidden layer Left
W_hidden = tf.Variable(tf.random.normal([784, num_hidden]))
b_hidden = tf.Variable(tf.random.normal([num_hidden]))
p_hidden = tf.nn.relu( tf.add(tf.matmul(X, W_hidden), b_hidden) )
W_hidden2 = tf.Variable(tf.random.normal([num_hidden, num_hidden]))
b_hidden2 = tf.Variable(tf.random.normal([num_hidden]))
p_hidden2 = tf.nn.relu( tf.add(tf.matmul(p_hidden, W_hidden2), b_hidden2) )
# Build a hidden layer Right
W_hiddenR = tf.Variable(tf.random.normal([784, num_hidden]))
b_hiddenR = tf.Variable(tf.random.normal([num_hidden]))
p_hiddenR = tf.nn.relu( tf.add(tf.matmul(X2, W_hiddenR), b_hiddenR) )
W_hidden2R = tf.Variable(tf.random.normal([num_hidden, num_hidden]))
b_hidden2R = tf.Variable(tf.random.normal([num_hidden]))
p_hidden2R = tf.nn.relu( tf.add(tf.matmul(p_hiddenR, W_hidden2R), b_hidden2R) )
# Conncatenate Left + Right
W_concat = tf.Variable(tf.random.normal([1000, num_hidden]))
b_concat = tf.Variable(tf.random.normal([num_hidden]))
p_concat2 = tf.nn.relu(tf.add(tf.matmul(tf.add(p_hidden2,p_hidden2R), W_concat), b_concat))
# Build the output layer
W_output = tf.Variable(tf.random.normal([num_hidden, 10]))
b_output = tf.Variable(tf.random.normal([10]))
p_output = tf.nn.softmax( tf.add(tf.matmul(p_concat2, W_output), b_output))
loss = tf.reduce_mean(tf.losses.mean_squared_error(
labels=Y,predictions=p_output))
accuracy=1-tf.sqrt(loss)
minimization_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
saver = tf.train.Saver()
def norm(x):
return (x-np.min(x))/(np.max(x)-np.min(x))
feed_dict = {
X: norm(x_train[0:1000].reshape(-1,784)),X2: norm(x_train[0:1000].reshape(-1,784)),
Y: pd.get_dummies(y_train[0:1000])
}
with tf.Session() as session:
session.run(tf.global_variables_initializer())
for step in range(8000):
J_value = session.run(loss, feed_dict)
acc = session.run(accuracy, feed_dict)
if step % 100 == 0:
print("Step:", step, " Loss:", J_value," Accuracy:", acc)
session.run(minimization_op, feed_dict)
pred00 = session.run([p_output], feed_dict={X: x_test.reshape(-1,784)})
但是,我收到以下错误:
InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: In[0] mismatch In[1] shape: 128 vs. 1000: [1000,128] [1000,128] 0 0
[[{{node MatMul_147}}]]
[[Mean_41/_25]]
(1) Invalid argument: In[0] mismatch In[1] shape: 128 vs. 1000: [1000,128] [1000,128] 0 0
[[{{node MatMul_147}}]]
0 successful operations.
0 derived errors ignored.
关于如何解决这个问题的任何想法?
解决方案
我会回答我的问题:我解决了这个问题,神经网络现在正在训练。该错误是指矩阵试图乘以 的MatMul
操作 。因为我想要一个 1000 x 128 的输出,所以我不得不乘以x将生成一个输出。[1000,128]
[1000,128]
[1000,128]
[128,128]
[m,n]
[n,p]
[m,p]
推荐阅读
- python - 具有空字符串的条件 f 字符串格式
- lua - 如何在 lua 上为 Awesome WM 正确编写我自己的热键?
- javascript - 多按钮选择场景
- javascript - JavaScript 中看似随机的 TypeError
- excel - 自动重组大型 Excel 表格,根据分组变量将每列分成多列
- ios - 错误 ITMS-90432:无效的 Swift 支持。有解决办法吗?
- visual-studio - CMake/CPack Visual Studio .msi 安装项目
- javascript - 窗口香草js上的关闭模式
- c++ - 如何在 vscode 中运行 gtk+ 的 cpp 应用程序?
- node.js - Angular + NodeJS HTTP 错误:“JSON.parse 中位置 0 处的 JSON 中的意外令牌 <”