Error while doing reshape

Viewed 665
from random import randint
from random import seed
import math
import numpy as np
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense,TimeDistributed,RepeatVector

seed(1)
def ele():
    X,y = [],[]
    for i in range(1):
        l1=[]
        for _ in range(2):
            l1.append(randint(1,10))
        X.append(l1)
        y.append(sum(l1))
    for i in range(1):
        X = str(X[0][0])+'+'+str(X[0][1])
        y = str(y[0])
    char_to_int = dict((c, i) for i, c in enumerate(alphabet))
    Xenc,yenc = [],[]
    for pattern in X:
        integer_encoded = [char_to_int[char] for char in pattern]
        Xenc.append(integer_encoded[0])
    for pattern in y:
        integer_encoded = [char_to_int[char] for char in pattern]
        yenc.append(integer_encoded[0])
    k,k1 = [],[]
    for i in range(1):
        for j in Xenc:
            vec = np.zeros(11)
            vec[j] = 1
            k.append(vec)
        for j in yenc:
            vec1 = np.zeros(11)
            vec1[j] = 1
            k1.append(vec1)
        k = np.array(k)
        k1 = np.array(k1)
    return k,k1

alphabet = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+']

model = Sequential()
model.add(LSTM(100, input_shape=(n_in_seq_length,11)))
model.add(RepeatVector(2))
model.add(LSTM(50, return_sequences=True))
model.add(TimeDistributed(Dense(n_chars, activation='softmax')))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

for i in range(1):
    X,y = ele()
    #X = np.reshape(X, (4,1,11))
    model.fit(X, y, epochs=1, batch_size=10)

I got this error:

ValueError Traceback (most recent call last) in () 53 X,y = ele() 54 #X = np.reshape(X, (4,1,11)) ---> 55 model.fit(X, y, epochs=1, batch_size=10)

~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs) 948 sample_weight=sample_weight, 949 class_weight=class_weight, --> 950 batch_size=batch_size) 951 # Prepare validation data. 952 do_validation = False

~\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size) 747 feed_input_shapes, 748 check_batch_axis=False, # Don't enforce the batch size. --> 749 exception_prefix='input') 750 751 if y is not None:

~\Anaconda3\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix) 125 ': expected ' + names[i] + ' to have ' + 126 str(len(shape)) + ' dimensions, but got array ' --> 127 'with shape ' + str(data_shape)) 128 if not check_batch_axis: 129 data_shape = data_shape[1:]

ValueError: Error when checking input: expected lstm_42_input to have 3 dimensions, but got array with shape (4, 11)

1 Answers

in the code is a problem with reshaping data. For reshaping cf Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (339732, 29) and https://github.com/keras-team/keras/issues/5214. In an python array the number of [ and ] indicates the dimensionality of an array

TimeDistributed layer needs at least two timesteps in your code you have one, in the code below timestep=3 is used because 33 is not divisible by 2. In general TimeDistributed layer is used to realize one-to-many and many-to-many configurations, cf https://github.com/keras-team/keras/issues/1029

the following code is working, the reshaping is done for 1 sample (batch_size), 3 timesteps, 11 features :

from random import randint
from random import seed
import math
import numpy as np
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense,TimeDistributed,RepeatVector

seed(1)
def ele():
    X,y = [],[]
    for i in range(1):
        l1=[]
        for _ in range(2):
            l1.append(randint(1,10))
        X.append(l1)
        y.append(sum(l1))
    for i in range(1):
        X = str(X[0][0])+'+'+str(X[0][1])
        y = str(y[0])
    char_to_int = dict((c, i) for i, c in enumerate(alphabet))
    Xenc,yenc = [],[]
    for pattern in X:
        integer_encoded = [char_to_int[char] for char in pattern]
        Xenc.append(integer_encoded[0])
    for pattern in y:
        integer_encoded = [char_to_int[char] for char in pattern]
        yenc.append(integer_encoded[0])
    k,k1 = [],[]
    for i in range(1):
        for j in Xenc:
            vec = np.zeros(11)
            vec[j] = 1
            k.append(vec)
        for j in yenc:
            vec1 = np.zeros(11)
            vec1[j] = 1
            k1.append(vec1)
        k = np.array(k)
        k1 = np.array(k1)
    return k,k1




alphabet = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+']
n_chars = 11

for i in range(1):
    X,y = ele()
    print('X not reshaped :', X)
    print('y not reshaped :', y)

    # timestep = 3, batch_size =1, input_dim = nb_features = 11
    X = np.reshape(X, (1,X.shape[0],X.shape[1]))
    y = np.reshape(y, (1,y.shape[0],y.shape[1]))
    print('X reshaped :', X)
    print('y reshaped :', y)
    print(' X.shape[0] :', X.shape[0])
    print(' X.shape[1] :', X.shape[1])
    print(' X.shape[2] :', X.shape[2])

model = Sequential()
model.add(LSTM(100, input_shape=(X.shape[1],X.shape[2])))
model.add(RepeatVector(2))
model.add(LSTM(50, return_sequences=True))
model.add(TimeDistributed(Dense(n_chars, activation='softmax')))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(X, y, epochs=1, batch_size=10)
Related