【问题标题】:value error :setting an array element with a sequence值错误:使用序列设置数组元素
【发布时间】:2018-09-01 17:43:20
【问题描述】:

我的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)

输出:

Traceback(最近一次调用最后一次): 文件“”,第 1 行,在 文件“”,第 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- packages/numpy/core/numeric.py”,第 531 行,在 asarray 中 返回数组(a,dtype,copy=False,order=order) ValueError: 使用序列设置数组元素。

请帮我解决这个错误

【问题讨论】:

    标签: python tensorflow deep-learning regression valueerror


    【解决方案1】:

    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]]
    

    【讨论】:

    • 谢谢马修先生。我忘记在其中添加一个元素。我会试试这个,希望这对我有用
    猜你喜欢
    • 2018-08-15
    • 1970-01-01
    • 2016-10-30
    • 2016-01-01
    • 2015-08-10
    • 2018-04-09
    • 1970-01-01
    • 2018-11-10
    相关资源
    最近更新 更多