返回 self._dims[key].value IndexError: list index

在张量流中,我制作了一个将哈希作为输入的常规网络。作为一个例子,我使用了内置的 python hash()函数(是的,它在每个会话中都改变了盐,但这是一个例子)代码是这样的:


from time import time

st = time()

import tensorflow as tf

print(time() - st)

import numpy as np

import chess

import atexit

from numpy import shape

data = open("data.data", "r").readlines()[:10000]

targets = open("targets.data", "r").readlines()[:10000]

boards_data = []

new_targets = []

for i in data:

    boards_data.append(hash(i))

for i in targets:

    new_targets.append(float(i))

print(len(new_targets))

print(len(boards_data))

print(np.array(new_targets))

print(np.array(boards_data))


def create_model():

   model = tf.keras.models.Sequential()

   model.add(tf.keras.layers.Reshape((1,1,1)))

   model.add(tf.keras.layers.Dense(1000, activation="tanh"))

   model.add(tf.keras.layers.Flatten())

   model.add(tf.keras.layers.Dense(1, activation='tanh'))

   model.compile(loss="mse", optimizer="adam", metrics=['accuracy'])

   return model


model = create_model()

model.fit(np.array(boards_data), np.array(new_targets), epochs=10)

model.predict(np.array(hash("8/6P1/5k1K/6r1/8/8/8/8 b - - 0 83")))

错误在预测中。我在如何修复 Tensorflow 中的“IndexError:列表索引超出范围”中看到了 conv2d 示例 ,但事实并非如此......


和回溯:


Traceback (most recent call last):

  File "/Volumes/POOPOO USB/lichess-bot/engines/engine2/nn_evaluation/nn_evaluation2.py", line 36, in <module>

    model.predict(np.array(hash("8/6P1/5k1K/6r1/8/8/8/8 b - - 0 83")))

  File "/Users/ofek/Library/Python/3.8/lib/python/site-packages/tensorflow/python/keras/engine/training.py", line 130, in _method_wrapper

    return method(self, *args, **kwargs)

  File "/Users/ofek/Library/Python/3.8/lib/python/site-packages/tensorflow/python/keras/engine/training.py", line 1569, in predict

    data_handler = data_adapter.DataHandler(

  File "/Users/ofek/Library/Python/3.8/lib/python/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1105, in __init__

    self._adapter = adapter_cls(


隔江千里
浏览 130回答 1
1回答

至尊宝的传说

问题是您正在从哈希值创建一个 0d numpy 字符串。预测只能在至少具有一维的数组上运行。您可以检查您的散列值是否为 0d:print(np.array(hash("8/6P1/5k1K/6r1/8/8/8/8&nbsp;b&nbsp;-&nbsp;-&nbsp;0&nbsp;83")).shape) #&nbsp;outputs:&nbsp;()与将哈希值放入列表相比:print(np.array([hash("8/6P1/5k1K/6r1/8/8/8/8&nbsp;b&nbsp;-&nbsp;-&nbsp;0&nbsp;83")]).shape) #&nbsp;outputs:&nbsp;(1,)第二个np.array预测运行没有错误。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python