繁体   English   中英

ValueError: 太多的值无法解压 3

[英]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)

文件“tf3_6.py”,第 13 行,Y = [[int(x0+x1<1)] for (x0,x1) in X] ValueError: too many values to unpack.

我认为代码没有错,但我仍然注意到值错误,所以我希望你们能帮助我解决这个问题,非常感谢

X的形状是(32, 3) ,但在您的列表理解中,您只是想解压 2 ​​个值:

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

要么改变你的兰特数组的形状:

X = rng.rand(32,2)

或者扔掉列表中的第三个兰特:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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