【发布时间】:2020-05-25 03:00:50
【问题描述】:
我收到了这个模糊不清的错误,我无法在网上找到解决方案。我正在尝试制作这段代码来找到两个数字之间的关系。出于测试目的,我使用了简单的数据,其中关系是简单地添加 5。这是我的训练数据的样子:-
0,5
1,6
2,7
3,8
4,9
...
现在,我正在尝试绘制一个简单的模型来完成我想做的事情。但是,它甚至没有进入代码的训练部分,只是以这个晦涩的错误终止:-
2020-05-22 20:00:25.521151: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2020-05-22 20:00:25.523749: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-05-22 20:00:25.523805: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]
Traceback (most recent call last):
File "main.py", line 34, in <module>
history = model.fit(train_data, epochs=100, verbose=1)
File "/home/awesom
2020-05-22 20:00:25.521151: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2020-05-22 20:00:25.523749: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-05-22 20:00:25.523805: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]
Traceback (most recent call last):
File "main.py", line 34, in <module>
history = model.fit(train_data, epochs=100, verbose=1)
File "/home/awe_ruler/.local/lib/python3.7/site-packages/keras/engine/training.py", line 1239, in fit
validation_freq=validation_freq)
File "/home/awesome_ruler/.local/lib/python3.7/site-packages/keras/engine/training_arrays.py", line 141, in fit_loop
if issparse(fit_inputs[i]) and not K.is_sparse(feed[i]):
IndexError: list index out of range
我不了解此错误的来源,因为 model.fit 使用的迭代器不是我编写的,而是从库本身使用的。作为参考,这是我的代码:-
# -*- coding: utf-8 -*-
# @author = Neel Gupta
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
import matplotlib.pyplot as pyplot
import csv
def csv_arr(csv_f):
results = []
# the loop..
with open(csv_f) as csvfile:
reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC) # change contents to floats
for row in reader: # each row is a list
results.append(row)
# returning the output
return np.asarray(results)
train_data = np.reshape(csv_arr("train.csv"), (15,2))
#x_data = Input(shape=(15,2))
def build_model():
model = Sequential([
Dense(4, input_shape=(2,), activation='relu', kernel_initializer='ones'),
Dense(1, activation='linear'),
])
return model
mymodel = build_model()
opt = Adam(lr=0.01, decay=0.009)
mymodel.compile(loss='mean_squared_error', optimizer=opt)
# fit model
history = mymodel.fit(train_data, epochs=50, verbose=1)
我尝试更改图层表示并调整其他内容,但错误仍然存在。我认为问题可能出在我的训练数据中,但我不确定。
这是我的训练数据(其中的一部分)输入模型时的样子:-
[[ 0. 5.]
[ 1. 6.]
[ 2. 7.]
[ 3. 8.]
[ 4. 9.]
[ 5. 10.]
原来,它实际上是一个csv文件,但为了方便使用,已经转换为NumPy数组。可能是数组格式不正确,但我对此一无所知。
我使用的是 Tensorflow GPU 2.2.0 版和 Keras 2.3 版。此外,我正在尝试使用按照 TensorFlow 官方网站的说明安装的 CUDA 10.1。我不知道我的错误还有什么其他原因。有人能解释一下吗?
【问题讨论】:
-
您似乎只传递了 X 值,为什么不传递 y 值呢?两者都需要训练神经网络
-
我觉得很难从这些信息中弄清楚到底发生了什么。您是否可以在文件中添加一个 pdb:/home/awesome_ruler/.local/lib/python3.7/site-packages/keras/engine/training_arrays.py 在第 141 行,看看 i 是什么?跨度>
-
此外,训练数据应包含 2 个数组 - 1 个用于 X,另一个用于 Y(具有相同的大小)。但是您提供的是一个列表列表,其中内部列表的每个数据都包含 (x,y)
-
@SaurabhVerma 我不明白你在说什么。您能否发布这是一个带有解释的答案并指出代码中需要更正的部分?
-
我已经提到过,model.fit 为 X 和 y 采用两个数组,而不是您提供的一个数组。大多数(如果不是全部)Keras 示例也使用两个数组
标签: python tensorflow machine-learning keras