简体   繁体   中英

ValueError: too many values to unpack 3

  1 i#:coding:utf-8
  2 #0导入模块,生成模拟数据集
  3 import tensorflow as tf
  4 import numpy as np
  5 BATCH_SIZE = 8
  6 seed = 23455
  7 
  8 #给予seed产生随机数
  9 rng = np.random.RandomState(seed)
 10 #随机数返回32行2列矩阵 表示32组 体积和重量 作为输入数据集
 11 X = rng.rand(32,3)
 12 
 13 Y = [[int(x0+x1<1)] for (x0,x1) in X]
 14 print "X:\n",X
 15 print "Y:\n",Y
 16 
 17 #1定义神经网络的输入,参数和输出,定义向前传播过程
 18 x = tf.placeholder(tf.float32, shape=(None, 2))
 19 y_= tf.placeholder(tf.float32, shape=(None, 1))
 20 
 21 w1= tf.variable(tf.random([2,3], stddev=1, seed=1))
 22 w2= tf.variable(tf.random([3,1], stddev=1, seed=1))
 23 
 24 a =tf.matmul(x,w1)
 25 y =tf.matmul(a,w2)
 26 
 27 #定义损失函数集反向传播方法
 28 loss = tf.reduce_mean(tf.square(y-y_))
 29 #train_step = tf.train.MomentumOptimizer(0.001,0.9).minimize(loss)
 30 #train_step = tf.train.AdamOptimizer(0.001).minimize(loss)
 31 
 32 #3生成会话,训练steps轮
 33 with tf.Session() as sess:
 34     init_op = tf.global_variables_initializer()
 35     sess.run(init_op)
 36     # 输出目前未经训练的参数取值。
 37     print "w1:\n", sess.run(w1)
 38     print "w2:\n", sess.run(w2)
 39     print "\n"
 40 
 41     #train the model
 42     STEPS=3000
 43     for i in range(STEPS):
 44         start =(i*BATCH_SIZE) % 32
 45         end = start + BATCH_SIZE
 46         sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})
 47         if i % 500 == 0:
 48             total_loss = sess.run(loss, feed_dict={x: X, y_: Y})
 49             print("After %d training steps(s), loss on all data in %g" % (i,     total_loss))
 50 
 51     #output the trained value of variables
 52     print "\n"
 53     print "w1:\n", sess.run(w1)
 54     print "w2:\n", sess.run(w2)

File "tf3_6.py", line 13, in Y = [[int(x0+x1<1)] for (x0,x1) in X] ValueError: too many values to unpack.

The code i don't think it is wrong but i still noticed the value error so i hope you guys to help me cope this question thanks a lot

The shape of X is (32, 3) , but in your list comprehension, you are only trying to unpack 2 values:

Y = [[int(x0+x1<1)] for (x0,x1) in X]

Either change the shape of your array of rands:

X = rng.rand(32,2)

Or throw away the third rand in your list comp:

Y = [[int(x0+x1<1)] for (x0,x1, _) in X]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM