我在学习代码时遇到了一些问题,因为执行时返回了以下错误
错误
Traceback (most recent call last):File "tf_apprentissage/main.py", line 109, in <module>f_de_x = model(x) # 用于确定图像是什么 (y = f(x) )File "tf_apprentissage/main.py", line 91, in modelc9 = dense(c7, (-1,100, 2))File "tf_apprentissage/main.py", line 61, in densew = tf.Variable(tf.truncated_normal(forme, stddev=(2/n)**.5)) # 权重File "/home/etudiant/PycharmProjects/tf_apprentissage/lib/python3.5/site-packages/tensorflow/python/ops/random_ops.py", line 169, in truncated_normalstddev_tensor = ops.convert_to_tensor(stddev, dtype=dtype, name="stddev")File "/home/etudiant/PycharmProjects/tf_apprentissage/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 836, in convert_to_tensoras_ref=False)File "/home/etudiant/PycharmProjects/tf_apprentissage/lib/python3.5/site- packages/tensorflow/python/framework/ops.py", line 926, in internal_convert_to_tensorret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)File "/home/etudiant/PycharmProjects/tf_apprentissage/lib/python3.5/site-packages/tensorflow/python/framework/constant_op.py", line 229, in _constant_tensor_conversion_functionreturn constant(v, dtype=dtype, name=name)File "/home/etudiant/PycharmProjects/tf_apprentissage/lib/python3.5/site-packages/tensorflow/python/framework/constant_op.py", line 208, in constantvalue, dtype=dtype, shape=shape, verify_shape=verify_shape))File "/home/etudiant/PycharmProjects/tf_apprentissage/lib/python3.5/site-packages/tensorflow/python/framework/tensor_util.py", line 383, in make_tensor_proto_AssertCompatible(values, dtype)File "/home/etudiant/PycharmProjects/tf_apprentissage/lib/python3.5/site-packages/tensorflow/python/framework/tensor_util.py", line 303, in _AssertCompatible(dtype.name, repr(mismatch), type(mismatch).__name__))TypeError: Expected float32, got (8.659560562354934e-17+1.4142135623730951j) of type 'complex' instead.
我是Tensorflow的新手,所以我不知道问题出在哪里,任何帮助我都会非常感激。
这段源代码的目标是识别单词,作为课堂项目的一部分,首先我们想要识别字母和数字。我之前从未做过任何机器学习项目,这是我第一次使用Tensorflow。我们使用手绘数字的图像作为输入,并带有标签。
我的代码是
# -*- coding: utf-8 -*- import tensorflow as tf from PIL import Image import numpy as np import os """ # 模型参数 W = tf.Variable([.3], dtype=tf.float32) b = tf.Variable([-.3], dtype=tf.float32) # 模型输入和输出 x = tf.placeholder(tf.float32) linear_model = W * x + b y = tf.placeholder(tf.float32) # 损失 loss = tf.reduce_sum(tf.square(linear_model - y)) # 平方和 # 优化器 optimizer = tf.train.GradientDescentOptimizer(0.01) train = optimizer.minimize(loss) # 训练数据 x_train = [1, 2, 3, 4] y_train = [0, -1, -2, -3] # 训练循环 init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) # 重置值为错误 for i in range(1000): sess.run(train, {x: x_train, y: y_train}) # 评估训练准确性 curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train}) print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss)) """ def conv(entree, noyaux): n = noyaux[0]*noyaux[1]*noyaux[2] W = tf.Variable(tf.truncated_normal(noyaux, stddev=(2/n)**.5)) # 随机权重(高斯分布) b = tf.Variable(tf.zeros(noyaux[-1:])) # 偏置 conv = tf.nn.conv2d(entree, W, [1, 1, 1, 1], 'SAME')+b # entree = 我们的图像, # W = 权重(我们的卷积核), # [1,1,1,1] = 我们的卷积步长, # 'SAME' 将在我们的矩阵周围添加零,以保持输出矩阵的大小相同 # 因此输出矩阵不会丢失信息 return tf.nn.relu(conv) # 应用非线性激活函数 def pool(entree): return tf.nn.max_pool( entree, [1, 2, 2, 1], [1, 2, 2, 1], 'SAME' ) def dense(entree, forme): n = forme[0] w = tf.Variable(tf.truncated_normal(forme, stddev=(2/n)**.5)) # 权重 b = tf.Variable(tf.zeros(forme[0])) matmul = tf.matmul(entree, w)+b return tf.nn.relu(matmul) def model(entree): c1 = conv(entree, (3, 3, 3, 16)) c2 = conv(c1, (3, 3, 16, 16)) c3 = pool(c2) c4 = conv(c3, (3, 3, 16, 32)) # 解释第二个参数 # 3 -> 长度 # 3 -> 宽度 # 16->前一个3D矩阵的深度 # 32->下一个3D矩阵的深度 c5 = conv(c4, (3, 3, 32, 32)) c6 = pool(c5) c7 = tf.reshape(c6, (-1, 16**2 * 32)) # 将我们的3D矩阵压平成一个向量 # c6 = 3D矩阵 # arg2 = 形状: # -1 : 唯一的解决方案 # 16**2 * 32 = 向量中的元素数量 c8 = dense(c7, (-1,16**2 * 32, 100)) # 矩阵乘法 c9 = dense(c8, (-1,100, 2)) return c9 def resize_img(img): image_temp = Image.open(img) image = tf.image.decode_png(content = image_temp, channels = 1, dtype = tf.float32) image = tf.image.resize_images(image_temp,[64,64]) return image_temp if __name__ == "__main__": x = tf.placeholder(tf.float32, [None, 64, 64, 3]) # 将包含我们的图像 # tf.float32 -> 存储的数据类型 # 参数2 将是x的形状 # None -> 表示同时处理多张图像 # 64, 64, 3 -> 图像分辨率:3 = 深度 path_to_dataset = "Hnd/Img/" f_de_x = model(x) # 用于确定图像是什么 (y = f(x) ) y = tf.placeholder(tf.float32, [None, 2]) # 标签(图像的实际意义) # 成本函数(计算我们想要的和我们得到的之间的距离) cout = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=f_de_x, labels=y)) step = tf.train.AdamOptimizer(0.0001).minimize(cout) # 最小化我们的成本(0.0001是任意的) with tf.Session() as session: session.run(tf.global_variables_initializer()) for dir in os.listdir(path_to_dataset): for file in os.listdir(path_to_dataset + dir): image = resize_img(path_to_dataset+dir+"/"+file) session.run(step, feed_dict={x: image, y: dir})
回答:
w = tf.Variable(tf.truncated_normal(forme, stddev=(2/n)**.5)) # 权重
forme
似乎是一个复数,在上面的代码中你引用了 forme[0]
,我猜这是复数的实部。而在上面的行中,forme
没有索引到复数的实部。
你是否想要这样:
w = tf.Variable(tf.truncated_normal(forme[0], stddev=(2/n)**.5)) # 权重
如果不是,请打印出 forme
的值,并验证它是否是你期望的。如果你不希望它是复数,请追踪这个问题。