简体   繁体   中英

Transforming data to Tensor for LSTM Model

I am trying to use NFL tracking data in a neural network to predict the yardage outcome of a play. For this, I am trying to use a Keras LSTM model.

My data is formatted such that train_x is a list of numpy arrays where each numpy array is data for a specific play. train_y is a list holding a list with the play outcomes

train_x = [[[a11,b11,c11],[a12,b12,c12],...],[[a21,b21,c21],[a22,b22,c22],...],...]

train_y = [[a1],[a2],...]

When I try to use this data to train model:

embedding_vecor_length = 32
model = Sequential()
model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(train_x, train_y, validation_data=(val_x, val_y), epochs=3, batch_size=64)
print(model.summary())

I get this error:

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

The language of this error confuses me, why would there be a problem converting a numpy array when the object type is a numpy array?

Full Code for context:

lastWeek = pd.read_csv(r".\week1.csv", low_memory=False)
gameMax = lastWeek['gameId'].max()
weekPicklePath = "./week_1.pkl"
playPicklePath = "./play_all.pkl"
modPicklePath = "./modPlays_1.pk1"
trainXPicklePath = "./trainX1_1.pk1"
trainYPicklePath = "./trainY1_1.pk1"
testXPicklePath = "./testX1_1.pk1"
testYPicklePath = "./testY1_1.pk1"
valXPicklePath = "./valX1_1.pk1"
valYPicklePath = "./valY1_1.pk1"

pickle = True
try:
    foo = pd.read_pickle(weekPicklePath)
except (OSError, IOError, FileNotFoundError) as e:
    pickle = False


print("starting data preparation")




if(pickle):
    week = foo
else:
    print("week pickle not found")
    week1 = pd.read_csv(r".\week1.csv", low_memory=False)
    week =  pd.concat([week1], ignore_index=True)
    week = week[week['gameId'] <= gameMax]
    week.to_pickle(weekPicklePath)


try:
    plays = pd.read_pickle(playPicklePath)
except (OSError, IOError, FileNotFoundError) as e:
    print("plays pickle not found")
    plays = pd.read_csv(r".\plays.csv", low_memory=False)
    plays.to_pickle(playPicklePath)

try:
    modPlays = pd.read_pickle(modPicklePath)
except (OSError, IOError, FileNotFoundError) as e:
    print("modPlays pickle not found")
    modPlays = plays[plays["gameId"] <= gameMax]
    modPlays.to_pickle(modPicklePath)

plays = modPlays

def isolatePlay(data, gameNum, playNum):
    MAX_X_YARDS = 120
    MAX_Y_YARDS = 53.3
    d = data[data['gameId'] == gameNum]
    d = d[d['playId'] == playNum].fillna(0)
    #normalize x ,y...
    sub = d[["x","y", "s", "a", "dis", "o", "dir"]].to_numpy()
    norm = Normalizer().fit(sub)


    return norm.transform(sub)



print("creating ML training, test, and validation datasets")
first = True
for  rows in plays.itertuples():
    #print(getattr(rows, 'gameId'), gameMax)
    play = isolatePlay(week, getattr(rows, 'gameId'), getattr(rows, 'playId'))
    if (first):
        x = [play]
        y = [[getattr(rows, 'offensePlayResult')]]
        first = False
    else:
        x.append(play)
        y.append([getattr(rows, 'offensePlayResult')])
train_x, test_x, train_y, test_y = train_test_split(np.array(x), np.array(y), test_size=0.3)
test_x, val_x, test_y, val_y = train_test_split(test_x, test_y, test_size=0.5)
print("x data:[0]", train_x[0])
print("x data:[1]", train_x[1])


print("ML Dataset Preparation Complete")



 # create the model
embedding_vecor_length = 32
model = Sequential()
model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(train_x, train_y, validation_data=(val_x, val_y), epochs=3, batch_size=64)
print(model.summary())

# Final evaluation of the model
scores = model.evaluate(test_x, test_y, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))

Full traceback:

Traceback (most recent call last):
  File "c:/Users/benja/source/repos/NFL/nflModelTest2.py", line 128, in <module>
    model.fit(train_x, train_y, validation_data=(val_x, val_y), epochs=3, batch_size=64)
  File "C:\Users\benja\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper
    return method(self, *args, **kwargs)
  File "C:\Users\benja\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1049, in fit
    data_handler = data_adapter.DataHandler(
  File "C:\Users\benja\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1105, in __init__
    self._adapter = adapter_cls(
  File "C:\Users\benja\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 265, in __init__
    x, y, sample_weights = _process_tensorlike((x, y, sample_weights))
  File "C:\Users\benja\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1021, in _process_tensorlike
    inputs = nest.map_structure(_convert_numpy_and_scipy, inputs)
  File "C:\Users\benja\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\util\nest.py", line 635, in map_structure
    structure[0], [func(*x) for x in entries],
  File "C:\Users\benja\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\util\nest.py", line 635, in <listcomp>
    structure[0], [func(*x) for x in entries],
  File "C:\Users\benja\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1016, in _convert_numpy_and_scipy
    return ops.convert_to_tensor(x, dtype=dtype)
  File "C:\Users\benja\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\ops.py", line 1499, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "C:\Users\benja\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\tensor_conversion_registry.py", line 52, in _default_conversion_function
    return constant_op.constant(value, dtype, name=name)
  File "C:\Users\benja\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 263, in constant
    return _constant_impl(value, dtype, shape, name, verify_shape=False,
  File "C:\Users\benja\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 275, in _constant_impl
    return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
  File "C:\Users\benja\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 300, in _constant_eager_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
  File "C:\Users\benja\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 98, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

I tried the solution in this post and it worked for me (Python 3.8.8 and Tensorflow 2.3.0).

I tried to run Anaconda Navigator as administrator. Then, I downgraded to numpy 1.18.5 using the following command on Jupyter:

! pip install numpy==1.18.5 --user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM