【问题标题】:TypeError: Expected binary or unicode string, got list TensorflowTypeError:预期的二进制或 unicode 字符串,得到列表 Tensorflow
【发布时间】:2017-12-01 21:18:22
【问题描述】:

好的,请耐心等待。所以这是我的代码:

import tensorflow as tf
import os
import pickle
from numpy import asarray, reshape

os.chdir('PATH')

with open('xSensor.pkl','rb') as file:
    x_train = asarray(pickle.load(file))

with open('ySensor.pkl','rb') as file:
    y_train = asarray(pickle.load(file))

def neural_network(data):
    n_nodes_h1 = 1000
    n_nodes_h2 = 1000
    n_nodes_h3 = 500

    hidden_layer_1 = {
        'weights': tf.Variable(tf.random_normal([13, n_nodes_h1],dtype=tf.float64)),
        'biases': tf.Variable(tf.random_normal([n_nodes_h1],dtype=tf.float64))
    }

    #Omitting some code that just defines a couple more layers in the same format as above

    layer_1 = tf.matmul(data, hidden_layer_1['weights']) + hidden_layer_1['biases']
    layer_1 = tf.nn.relu(layer_1)

    #Omitting more code.

    output = tf.matmul(layer_3, output['weights']) + output['biases']

    return output


def train_network(x_t,y_t):
    x = tf.placeholder(tf.float64, shape=[None, 13])
    y = tf.placeholder(tf.float64)
    prediction = neural_network(x_t)
    y_t = reshape(y_t,(700,1))
    cost = tf.reduce_mean(tf.losses.mean_squared_error(labels=y_t, predictions=prediction))
    optimizer = tf.train.AdamOptimizer(0.005).minimize(cost) #learning rate by default is 0.01
    n_epochs = 1000

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for _ in range(0, n_epochs):
            x_ = sess.run([optimizer,cost], feed_dict={x: x_t, y: y_t})
            print("Loss is: ", x_[1])



train_network(x_train,y_train)

这是错误日志:

Traceback (most recent call last):
  File "C:/Users/my system/Desktop/height_sensor.py", line 94, in <module>
    train_network(x_train,y_train)
  File "C:/Users/my system/Desktop/height_sensor.py", line 77, in train_network
    prediction = neural_network(x_t)
  File "C:/Users/my system/Desktop/height_sensor.py", line 59, in neural_network
    layer_1 = tf.matmul(data, hidden_layer_1['weights']) + hidden_layer_1['biases']
  File "C:\Users\my system\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1844, in matmul
    a = ops.convert_to_tensor(a, name="a")
  ...etc...
TypeError: Expected binary or unicode string, got [15.0126, 1.38684, 27.6, 1.6323, -0.624113, 8.97763, 2.06581, 8.88303, -0.689839, 9.13284, 353.183, 349.178, 210.498]

Process finished with exit code 1

很抱歉发布了这么多代码,我想少发布 waaaay,但我担心我可能会忽略导致错误的一件事。 如果有人能指出我正确的方向,我会非常高兴。 谢谢

【问题讨论】:

    标签: python-3.x debugging tensorflow


    【解决方案1】:

    发布所有代码实际上是一个好主意,尤其是所有错误回溯。我可以让您的代码使用一些假数据运行。我最好的猜测是pickle返回x_train的值的方式有些不对劲;可能数组有太多嵌套。我可能可以在整个回溯方面提供更多帮助,但无论如何我建议输出一些 x_train 并查看其格式是否正确。

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      相关资源
      最近更新 更多