我的linear regression
代码,包含3个功能。我试图在终端执行。我收到以下错误
值错误:使用序列设置数组
import tensorflow as tf
INPUT_XOR=[[1,72,50,33.6],[1,66,31,26.6],[1,64,32,23.3],
[1,66,21,28.1],[1,40,33,43.1],[1,74,30,25.6],[1,50,26,31.0],
[1,0,29,35.3],[1,70,53,30.5],[96,54,0]]
OUTPUT_XOR=[[148],[85],[183],[89],[137],[116],[78],[115],[197],[125]]
n_nodes_hl1=10
n_nodes_hl2=1
batch_size=100
x=tf.placeholder('float',[10,4])
y=tf.placeholder('float',[10,1])
def train_neural_network(x):
hidden_1_layer=
{'weights':tf.Variable(tf.random_uniform([4,n_nodes_hl1],-1.0,1.0)),
'biases':tf.Variable(tf.zeros([n_nodes_hl1]))}
output_layer=
{'weights':tf.Variable(tf.random_uniform([n_nodes_hl1,n_nodes_hl2],
-1.0,1.0)),
'biases':tf.Variable(tf.zeros([n_nodes_hl2]))}
l1=tf.add(tf.matmul(x,hidden_1_layer['weights']),hidden_1_layer['biase
s'])
l1=tf.nn.relu(l1)
output=tf.add(tf.matmul(l1,output_layer['weights']),output_layer['biases'])
output=tf.sigmoid(output)
prediction=output
cost=tf.reduce_mean(tf.squared_difference(prediction,y))
optimizer=tf.train.GradientDescentOptimizer(0.01).minimize(cost)
hm_epochs=10000
with tf.Session() as sess:
init_op=tf.global_variables_initializer()
sess.run(init_op)
for epoch in range(100001):
epoch_loss=0
sess.run(optimizer, feed_dict={x:INPUT_XOR, y:OUTPUT_XOR})
if epoch%10000==0:
c=sess.run(cost, feed_dict={x:INPUT_XOR, y:OUTPUT_XOR})
#epoch_loss+=c
print('Epoch:', epoch, 'completed out of ',hm_epochs
,'cost', c)
print('_'*80)
for element in sess.run(prediction, feed_dict=
{x:INPUT_XOR, y:OUTPUT_XOR}):
print(' ', element)
correct=tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(correct,'float'))
print('Accuracy:',accuracy.eval({x:INPUT_XOR, y:OUTPUT_XOR}))
train_neural_network(x)
输出:
追踪(最近一次通话): 文件“”,第1行,in 文件“”,第19行,在train_neural_network中 文件“/home/vj/tensorflow/local/lib/python2.7/site- packages / tensorflow / python / client / session.py“,第895行,运行中 run_metadata_ptr) 文件“/home/vj/tensorflow/local/lib/python2.7/site- packages / tensorflow / python / client / session.py“,第1093行,在_run中 np_val = np.asarray(subfeed_val,dtype = subfeed_dtype) 文件“/home/vj/tensorflow/local/lib/python2.7/site- package / numpy / core / numeric.py“,第531行,在asarray中 返回数组(a,dtype,copy = False,order = order) ValueError:使用序列设置数组元素。
请帮我解决此错误
答案 0 :(得分:0)
INPUT_XOR
矩阵的最后一行有三个元素而不是四个元素。试试这段代码:
INPUT_XOR = [[1,72,50,33.6], [1,66,31,26.6], [1,64,32,23.3], [1,66,21,28.1], [1,40,33,43.1], [1,74,30,25.6], [1,50,26,31.0], [1,0,29,35.3], [1,70,53,30.5], [96,54,0,0.0]]