gpt4 book ai didi

python - 使用 tensorflow 拟合神经网络模型会引发错误

转载 作者:行者123 更新时间:2023-12-02 02:33:52 24 4
gpt4 key购买 nike

我正在关注this tutorial在实现一个简单的神经网络时,它将从 csv 文件读取数据。这是存储在 games.csv

中的数据
white_pawns, black_pawns, white_queens, black_queens,result
3, 4, 1, 1, -1
3, 5, 1, 2, -1
6, 4, 0, 0, 1
2, 2, 3, 1, 1
5, 2, 0, 3, -1
10, 10, 0, 0, 1
8, 10, 0, 1, -1

这是按照说明创建的代码。我的列已经是数字了,所以我跳过了教程的这一部分。

import tensorflow as tf

def pack(features, label):
return tf.stack(list(features.values()), axis=-1), label

labelColumn = 'result'
labels = [-1, 1]

train_file_path = "C:/games.csv"

games_train = tf.data.experimental.make_csv_dataset(
train_file_path,
batch_size=5,
label_name=labelColumn,
na_value="?",
num_epochs=1,
ignore_errors=True
)

packed_dataset = games_train.map(pack)
train_data = packed_dataset.shuffle(500)

model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(4,)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])

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

model.fit(train_data, epochs=1)

看起来数据读取正确,但是当尝试调用 model.fit(train_data, epochs=1) 时出现错误:

    2/Unknown - 2s 1s/step - loss: 1.0507 - accuracy: 0.0000e+002020-02-02 15:14:00.577149: W tensorflow/core/common_runtime/base_collective_executor.cc:216] BaseCollectiveExecutor::StartAbort Out of range: End of sequence
[[{{node IteratorGetNext}}]]
2/2 [==============================] - 2s 1s/step - loss: 1.0507 - accuracy: 0.0000e+00
Exception ignored in: <function _RandomSeedGeneratorDeleter.__del__ at 0x000001FAF02041F8>
Traceback (most recent call last):
File "C:\Program Files\Anaconda\lib\site-packages\tensorflow_core\python\data\ops\dataset_ops.py", line 3009, in __del__
AttributeError: 'NoneType' object has no attribute 'device'
Exception ignored in: <function _RandomSeedGeneratorDeleter.__del__ at 0x000001FAF02041F8>
Traceback (most recent call last):
File "C:\Program Files\Anaconda\lib\site-packages\tensorflow_core\python\data\ops\dataset_ops.py", line 3009, in __del__
AttributeError: 'NoneType' object has no attribute 'device'
Exception ignored in: <function _RandomSeedGeneratorDeleter.__del__ at 0x000001FAF02041F8>
Traceback (most recent call last):
File "C:\Program Files\Anaconda\lib\site-packages\tensorflow_core\python\data\ops\dataset_ops.py", line 3009, in __del__
AttributeError: 'NoneType' object has no attribute 'device'

我在这里做错了什么以及如何解决它?

最佳答案

您使用的 Tensorflow 版本是什么? Tf2.0 这可能不是问题。

看来这可能是TF的一个bug,要纠正,你可以看看编辑site-packages中的tensorflow代码;

dataset_ops.py中,在文件顶部添加

import tensorflow as tf

然后在第 2944 行和第 3009 行替换:

with ops.device(self._device):

with tf.device(self._device):

请参阅此处的错误报告;

https://github.com/tensorflow/tensorflow/issues/35326

关于python - 使用 tensorflow 拟合神经网络模型会引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60027013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com