Reputation: 9
I am getting this obscure error for which I could find no solution online. I am trying to make this piece of code that finds the relation between two numbers. For testing purposes, I am using simple data where the relation is to simply add 5. Here is how my training data looks like:-
0,5
1,6
2,7
3,8
4,9
...
Now, I am trying to sketch out a simple model that accomplishes what I want to do. However, It does not even get to the training part of the code and simply terminates with this obscure error:-
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
I am not understanding the source of this error as the iterator that model.fit is using was not written by me but is being used from the libraries themselves. For reference, here is my code:-
# -*- 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)
I tried changing the layer representation and tweaking other things but the error persists. I think that the problem may be in my training data but I am unsure.
This is how my training data looks like (a part of it) when it is being fed into the model:-
[[ 0. 5.]
[ 1. 6.]
[ 2. 7.]
[ 3. 8.]
[ 4. 9.]
[ 5. 10.]
Originally, it is actually a csv
file but has been converted to a NumPy array for easy use. It could be that there the array is not well-formed however I have no clue about this.
I am using Tensorflow GPU version 2.2.0 and Keras version 2.3. Additionally, I am trying to use CUDA 10.1 which was installed by following instructions from official TensorFlow site. I do not know what other cause there is to my error. Can someone shed any light on this?
Upvotes: 0
Views: 406
Reputation: 11377
In your training data the first column is the input, while the second is the target variable. In the code you have presented you treat both as am input, without presenting the target variable.
If you split the training data into input and target variable, all will work just fine (in terms of training process, the results won't be spectacular).
Here's a full working example:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
X = np.random.randint(0, 100, 1500)
train_data = np.array([X, X+5])
def build_model():
model = Sequential([
Dense(1, input_shape=(1,), 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(x=train_data[0], y=train_data[1], epochs=50, verbose=1)
Note I simulated the training data and changed input_shape
on the build_model
.
Upvotes: 1