1 year ago

#277124

test-img

Sudan pokharel

Batch size cannot be greater than 1 after using custom loss function

I have a custom loss function in LSTM. The model run wells while the batch size is 1 but give me error Input to reshape is a tensor with 2 values, but the requested shape has 1 [[{{node loss/Reshape}}]] [Op:__inference_train_function_3093102] when i increase the batch size. what could be done to use different batch size ? I appreciate your help. """ def custom_loss(sumP,sumE):

    def loss(y_true, y_pred):
        penalty= 0.69
        y_pred =(y_pred*data_train_std[-1])+data_train_mean[-1]
        y_pred = y_pred*3.54E-05
        y_pred1 = K.sum(y_pred,axis=-1)
        

        y_true =( y_true*data_train_std[-1])+data_train_mean[-1]
        y_true = y_true*3.54E-05
        y_true1 = K.sum(y_true,axis=-1)
        

        
        if (abs((sumP-sumE-(y_pred1))-(sumP-sumE-(y_true1)))) <= (abs(sumP-sumE-(y_true1))*0.01):
            return K.mean(K.abs(y_pred - y_true), axis=-1) 
        else:
            return K.mean(K.abs(y_pred - y_true), axis=-1)+(penalty*(abs((sumP-sumE-(y_pred1))-(sumP-sumE-(y_true1)))))
    return loss





# CONFIGURE LSTM --------------------------------------------------------------

model = Sequential()
model.add(LSTM(80, activation='relu', input_shape=(tlag, nvar), return_sequences=True))
model.add(LSTM(60, activation='relu', return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(20, activation='relu', return_sequences=False))
model.add(Dropout(0.3))
model.add(Dense(1, activation='linear'))
# opt=optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0, amsgrad=False)
model.compile(optimizer='adam', loss=custom_loss(sump1, sume1), metrics=['mse', 'mae'])
model.summary()


# TRAIN LSTM ------------------------------------------------------------------

result = model.fit(Xtrain, Ytrain, epochs=50, batch_size=2, validation_split=0.3, shuffle=False, verbose=True)

Error: Input to reshape is a tensor with 2 values, but the requested shape has 1 [[{{node loss/Reshape}}]] [Op:__inference_train_function_3093102]

tensorflow

loss-function

batchsize

0 Answers

Your Answer

Accepted video resources