Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following code:

def kerasModelGeneral():
    input_layer = keras.layers.Input(shape=(1, 4), name='input_shape')
    x1 = keras.layers.LSTM(100, return_sequences=False, name='lstm_0')(input_layer)
    x1 = keras.layers.Dropout(0.2, name='lstm_dropout')(x1)
    x2 = keras.layers.LSTM(10, return_sequences=False, activation="tanh", name='LSTM2')(input_layer)
    x2 = keras.layers.Dense(32, name="dense_LSTM2")(x2)
    x = keras.layers.Concatenate(-1)([x1, x2])
    x = keras.layers.Dense(64, name='x2')(x)
    output = keras.layers.Dense(4, activation='linear', name='x3')(x)
    model = keras.Model(inputs=input_layer, outputs=output)
    
    adam = keras.optimizers.Nadam(lr=0.001)
    model.compile(optimizer=adam, loss='mse')
    
    return model


window = 30
model = kerasModelGeneral()

model.train_on_batch(np.reshape(X_train, (window, 1, 4)), np.reshape(Y_train, (window, 1, 4)))
# X_train.shape = Y_train.shape = (30, 1, 4)

The model works fine, it does not give me any error. The problem comes when I try to train it. It gives me this error:

c:usersomarappdatalocalprogramspythonpython37libsite-packageskerasengineraining.py in train_on_batch(self, x, y, sample_weight, class_weight, reset_metrics)
   1506             x, y,
   1507             sample_weight=sample_weight,
-> 1508             class_weight=class_weight)
   1509         if self._uses_dynamic_learning_phase():
   1510             ins = x + y + sample_weights + [1]

c:usersappdatalocalprogramspythonpython37libsite-packageskerasengineraining.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
    619                 feed_output_shapes,
    620                 check_batch_axis=False,  # Don't enforce the batch size.
--> 621                 exception_prefix='target')
    622 
    623             # Generate sample-wise weight values given the `sample_weight` and

c:usersappdatalocalprogramspythonpython37libsite-packageskerasengineraining_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    133                         ': expected ' + names[i] + ' to have ' +
    134                         str(len(shape)) + ' dimensions, but got array '
--> 135                         'with shape ' + str(data_shape))
    136                 if not check_batch_axis:
    137                     data_shape = data_shape[1:]

ValueError: Error when checking target: expected x3 to have 2 dimensions, but got array with shape (30, 1, 4)

I'm aware of LSTM input, that is, if I'm not wrong, (batch, time steps, features). I have an unknown number of training data, but at once I'm passing 30 rows of 4 features each one.

This is an example of X__train (same shape for Y_train):

array([[[1.53570860e+00, 3.83927150e+00, 3.83927150e+00, 6.00000000e+03]],

       [[2.76604066e+00, 5.12197064e+00, 7.67854300e+00, 2.32000000e+02]],

       [[3.74148040e+00, 5.75228782e+00, 7.66089118e+00, 2.50000000e+03]],

       [[4.52006701e+00, 6.12871294e+00, 7.64323935e+00, 2.45030000e+04]],

       [[5.14293630e+00, 6.37966302e+00, 7.63882640e+00, 1.47730000e+04]],

       [[5.64123172e+00, 6.55891308e+00, 7.63441344e+00, 3.00000000e+02]],

       [[6.03986807e+00, 6.69335063e+00, 7.63441344e+00, 6.01400000e+03]],

       [[6.35914783e+00, 6.79811910e+00, 7.63441344e+00, 2.60000000e+03]],

       [[6.61420095e+00, 6.88174854e+00, 7.63441344e+00, 9.92800000e+03]],

       [[6.81824345e+00, 6.95017262e+00, 7.63441344e+00, 9.00000000e+02]],

       [[6.98059486e+00, 7.00682494e+00, 7.63441344e+00, 6.36600000e+03]],

       [[7.11135857e+00, 7.05510098e+00, 7.63441344e+00, 2.70000000e+03]],

       [[7.21596955e+00, 7.09648044e+00, 7.63441344e+00, 8.61200000e+03]],

       [[7.29789314e+00, 7.13175425e+00, 7.63441344e+00, 3.34500000e+03]],

       [[7.36343202e+00, 7.16261883e+00, 7.63441344e+00, 1.82800000e+03]],

       [[7.41586312e+00, 7.18985228e+00, 7.63441344e+00, 1.05440000e+04]],

       [[7.45604282e+00, 7.21356947e+00, 7.63441344e+00, 1.40000000e+03]],

       [[7.48818658e+00, 7.23479011e+00, 7.63441344e+00, 3.22240000e+04]],

       [[7.51566677e+00, 7.25432998e+00, 7.63441344e+00, 1.90000000e+03]],

       [[7.53588574e+00, 7.63516806e+00, 7.63441344e+00, 4.00000000e+03]],

       [[7.55206092e+00, 7.63207899e+00, 7.63441344e+00, 7.97400000e+03]],

       [[7.56323587e+00, 7.62810733e+00, 7.63441344e+00, 4.25700000e+03]],

       [[7.57217584e+00, 7.62634215e+00, 7.63220696e+00, 6.61900000e+03]],

       [[7.57756263e+00, 7.62457696e+00, 7.63000049e+00, 7.00000000e+02]],

       [[7.58363725e+00, 7.62325308e+00, 7.62779401e+00, 2.02600000e+03]],

       [[7.58849694e+00, 7.62192919e+00, 7.62558753e+00, 2.55790000e+04]],

       [[7.59238469e+00, 7.62060530e+00, 7.62558753e+00, 1.42310000e+04]],

       [[7.59549490e+00, 7.61918874e+00, 7.62558753e+00, 2.67730000e+04]],

       [[7.59798306e+00, 7.61786486e+00, 7.62558753e+00, 8.40100000e+03]],

       [[7.60262136e+00, 7.61720291e+00, 7.62558753e+00, 6.00000000e+02]]])

I've checked multtiple questions here at stackoverflow but I couldn't fin any valid answer to me, or I'm just blind enough to not see it. What is the problem there? What am I doing wrong?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.4k views
Welcome To Ask or Share your Answers For Others

1 Answer

Are you sure you posted the correct code?

I tried your code and did not have any issues. Code below, you can copy and try it. Only difference is that I generated some random data.

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
from matplotlib import pyplot as plt
import numpy as np


def kerasModelGeneral():
    input_layer = keras.layers.Input(shape=(1, 4), name='input_shape')
    x1 = keras.layers.LSTM(100, return_sequences=False, name='lstm_0')(input_layer)
    x1 = keras.layers.Dropout(0.2, name='lstm_dropout')(x1)
    x2 = keras.layers.LSTM(10, return_sequences=False, activation="tanh", name='LSTM2')(input_layer)
    x2 = keras.layers.Dense(32, name="dense_LSTM2")(x2)
    x = keras.layers.Concatenate(-1)([x1, x2])
    x = keras.layers.Dense(64, name='x2')(x)
    output = keras.layers.Dense(4, activation='linear', name='x3')(x)
    model = keras.Model(inputs=input_layer, outputs=output)
    
    adam = keras.optimizers.Nadam(lr=0.001)
    model.compile(optimizer=adam, loss='mse')
    
    return model

m = kerasModelGeneral()

window = 30
model = kerasModelGeneral()
print(m.summary())

X_train = tf.random.normal(shape=(30,1,4))
Y_train = tf.random.normal(shape=(30,1,4))

model.train_on_batch(np.reshape(X_train, (window, 1, 4)), np.reshape(Y_train, (window, 1, 4)))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...