【问题标题】:TFLearn cannot feed Y value properlyTFLearn 无法正确输入 Y 值
【发布时间】:2019-05-11 22:59:36
【问题描述】:

我正在尝试创建一个 AI 来使用 tensorflow 和 TFLearn 预测 FRC 比赛的结果。

以下是相关代码:

x = np.load("FRCPrediction/matchData.npz")["x"]
y = np.load("FRCPrediction/matchData.npz")["y"]

def buildModel():
    net = tflearn.input_data(shape = [None, 36])
    net = tflearn.fully_connected(net, 64)
    net = tflearn.dropout(net, 0.5)
    net = tflearn.fully_connected(net, 128, activation = "linear")
    net = tflearn.regression(net, optimizer='adam', loss='categorical_crossentropy')
    net = tflearn.fully_connected(net, 1, activation = "linear")
    model = tflearn.DNN(net)
    return model

model = buildModel()

BATCHSIZE = 128

model.fit(x, y, batch_size = BATCHSIZE)

失败并出现错误:

---------------------------------
Run id: 67BLHP
Log directory: /tmp/tflearn_logs/
---------------------------------
Training samples: 36024
Validation samples: 0
--
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-1b097e6d2ec5> in <module>()
      1 for i in range(EPOCHS):
----> 2     history = model.fit(x, y, batch_size = BATCHSIZE)
      3     print(history)

4 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1126                              'which has shape %r' %
   1127                              (np_val.shape, subfeed_t.name,
-> 1128                               str(subfeed_t.get_shape())))
   1129           if not self.graph.is_feedable(subfeed_t):
   1130             raise ValueError('Tensor %s may not be fed.' % subfeed_t)

ValueError: Cannot feed value of shape (128,) for Tensor 'TargetsData/Y:0', which has shape '(?, 128)'

非常感谢任何帮助。

【问题讨论】:

    标签: python tensorflow machine-learning artificial-intelligence tflearn


    【解决方案1】:

    为了计算所有变量的梯度,优化器应该在所有层之后使用高级 TFlearn API,有点来自 Tensorflow 的编码风格(我们这样做的低级 tf)。 Doc 很好地说明了它是如何工作的,也许你应该看看或搜索这个 API 的其他教程。要回答您的问题,请尝试:

    import tflearn
    import numpy as np
    
    x = np.ones((1000, 36))
    y = np.zeros((1000, 1))
    
    def buildModel():
        net = tflearn.input_data(shape=[None, 36])
        net = tflearn.fully_connected(net, 64)
        net = tflearn.dropout(net, 0.5)
        net = tflearn.fully_connected(net, 128, activation="linear")
        net = tflearn.fully_connected(net, 1, activation="linear")
        net = tflearn.regression(net, optimizer='adam', loss='categorical_crossentropy')
        model = tflearn.DNN(net)
        return model
    
    model = buildModel()
    
    BATCHSIZE = 128
    
    model.fit(x, y, batch_size=BATCHSIZE)
    

    【讨论】:

    • 你能显示y.shape吗?我明天更新
    • 确定 tf.shape(y) 返回 Tensor("Shape_2:0", shape=(1,), dtype=int32)。 y.shape 返回 (36024,)
    • 有趣。 @Zézouille 你是如何运行它的?你能用这个模型进行训练迭代吗?
    • 我尝试使用x = np.ones((36024, 36))y = np.zeros((36024, 1))。你可以使用y.reshape((36024, 1))
    • 您是否对代码进行了任何其他更改?我尝试使用更改运行它,但它抛出了同样的错误。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 2016-07-30
    相关资源
    最近更新 更多