我是TensorFlow的新手,需要为回归任务实施深度神经网络。我假设在互联网上没有这样的示例代码,使用深度神经网络进行回归(至少我找不到任何。如果有的话,请发布任何有用的链接)。所以,为了我的目的,我试图将深度神经网络上的教程合并为一起进行分类和回归。正如所料,我被错误轰炸。错误消息显示为:
InvalidArgumentError: In[0] is not a matrix
[[Node: MatMul_35 = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_Placeholder_36_0, Variable_72/read)]]
代码:
import tensorflow as tf
import numpy
import matplotlib.pyplot as plt
n_nodes_hl1 = 100
n_nodes_hl2 = 100
batch_size = 100
n_input = 1;
n_output = 1;
learning_rate = 0.01
train_X = numpy.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_Y = numpy.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
2.827,3.465,1.65,2.904,2.42,2.94,1.3])
x = tf.placeholder('float')
y = tf.placeholder('float')
def neural_network_model(data):
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([n_input, n_nodes_hl1])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']), hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
output = tf.reduce_sum(l2)
return output
def train_neural_network(x):
prediction = neural_network_model(x)
cost = tf.square(y - prediction)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
hm_epochs = 5
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
for (X, Y) in zip(train_X, train_Y):
_, c = sess.run([optimizer, cost], feed_dict={x: X, y: Y})
epoch_loss += c
print('Epoch', epoch, 'completed out of',hm_epochs,'loss:',epoch_loss)
plt.plot(train_X, train_Y, 'ro', label='Original data')
plt.plot(train_X, prediction, label='Fitted line')
plt.legend()
plt.show()
test_X = numpy.asarray([6.83, 4.668, 8.9, 7.91, 5.7, 8.7, 3.1, 2.1])
test_Y = numpy.asarray([1.84, 2.273, 3.2, 2.831, 2.92, 3.24, 1.35, 1.03])
print("Testing Data")
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
print('Accuracy:',accuracy.eval({x:test_X, y:test_Y}))
train_neural_network(x)
到目前为止,我猜隐藏图层权重和/或偏差的尺寸存在问题(我可能错了)。请帮我解决这个问题。谢谢!!
旁注:在这里,我刚试图制作一个简单的项目模型,其中的训练和测试数据点来自互联网示例。我的实际数据是几张图像的像素值。
答案 0 :(得分:2)
更改此行(为我工作):
matmul()函数的输入应该是一个矩阵 - 您正在提供一个值。
('Epoch', 0, 'completed out of', 5, 'loss:', array([[ 1.20472407e+14]], dtype=float32))
('Epoch', 1, 'completed out of', 5, 'loss:', array([[ 6.82631159]], dtype=float32))
('Epoch', 2, 'completed out of', 5, 'loss:', array([[ 8.83840561]], dtype=float32))
('Epoch', 3, 'completed out of', 5, 'loss:', array([[ 8.00222397]], dtype=float32))
('Epoch', 4, 'completed out of', 5, 'loss:', array([[ 7.6564579]], dtype=float32))
输出:
@FindBy(xpath = "(//div[@class='del-hours' and contains(.,'Store #50')]//..//..//..//..//i)[2]")
WebElement selectStore;
希望这有帮助!
评论:如果你去上班,这不是一个好的例子。 与图像。