Skip to main content

InvalidArgumentError: Expected multiples argument to be a vector of length 1 but got length 2 (LSTM)

I am trying to develop a LSTM model which can predict a target variable from predictors. Here is my LSTM code:

import pandas as pd
from pandas import DataFrame
from pandas import concat
from pandas import read_csv
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
import keras
import keras.backend as K
from keras.models import Sequential
from keras.layers import Dense, SimpleRNN, Dropout, LSTM
from keras.layers import Activation
from keras.utils import plot_model, np_utils
from keras.regularizers import L1L2
from hyperas import optim
#from hyperas.distributions import choice, uniform, conditional
from hyperas.distributions import choice, uniform
from hyperopt import Trials, STATUS_OK, tpe
from math import sqrt
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import random as rn
import os
matplotlib.rcParams.update({'font.size': 8})

#functions
def series_to_supervised(data, n_in=1,  
                         n_out=1,
                         dropnan=True):
    n_vars = 1 if type(data) is list else data.shape[1]
    df = DataFrame(data)
    cols, names = list(), list()
    # input sequence (t-n, ... t-1)
    for i in range(n_in, 0, -1):
        cols.append(df.shift(i))
        names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
    # forecast sequence (t, t+1, ... t+n)
    for i in range(0, n_out):
        cols.append(df.shift(-i))
        if i == 0:
            names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
        else:
            names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
    # put it all together
    agg = concat(cols, axis=1)
    agg.columns = names
    # drop rows with NaN values
    if dropnan:
        agg.dropna(inplace=True)
    return agg

def rmse(y_true, y_pred):
    return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1))

def mse(y_true, y_pred):
    return K.mean(K.square(y_pred - y_true), axis=-1)

def mae(y_true, y_pred):
    return K.mean(K.abs(y_pred - y_true), axis=-1)

def data():
    def series_to_supervised(data, n_in=1, 
                             n_out=1, 
                             dropnan=True):
        n_vars = 1 if type(data) is list else data.shape[1]
        df = DataFrame(data)
        cols, names = list(), list()
        for i in range(n_in, 0, -1):
            cols.append(df.shift(i))
            names += [('var%d(t-%d)' % (j + 1, i)) for j in range(n_vars)]
        for i in range(0, n_out):
            cols.append(df.shift(-i))
            if i == 0:
                names += [('var%d(t)' % (j + 1)) for j in range(n_vars)]
            else:
                names += [('var%d(t+%d)' % (j + 1, i)) for j in range(n_vars)]
        agg = concat(cols, axis=1)
        agg.columns = names
        if dropnan:
            agg.dropna(inplace=True)
        return agg

    n_lags = 12
    n_train = 360
    n_test = 60
    
    dataset_raw = read_csv("D:/Binata/bin_ann_lstm_trial/data_Q/NCEP_1984_2018_1.csv",
                           index_col=None, parse_dates=True, infer_datetime_format=True)
    
    train_dates = dataset_raw[['Datetime', 'Q', 'pr']].iloc[:n_train]
    test_dates = dataset_raw[['Datetime', 'Q', 'pr']].iloc[n_train:]
    
    test_dates = test_dates.reset_index(drop=True)
    test_dates['Datetime'] = pd.to_datetime(test_dates['Datetime'])
    
    dataset = dataset_raw.drop(dataset_raw.columns[[0]], axis=1)
    values = dataset.values  # to make just values
    values = values.astype('float32')
    
    Q = values[:, 0]  # to make just values
    Q = Q.reshape(Q.shape[0], 1)
    pr = values[:, 2]  # to make just values
    pr = pr.reshape(pr.shape[0], 1)
    
    Q_scaler, pr_scaler= MinMaxScaler(), MinMaxScaler()  # scaling wrt to max min
    
    Q_scaled = Q_scaler.fit_transform(Q)  # scaling
    pr_scaled = pr_scaler.fit_transform(pr)  # scaling
  
 
    Q_super1 = series_to_supervised(Q_scaled, n_lags) #need to delete first two columns and first two rows
    Q_super_values = Q_super1.values  # to make just values
    print(Q_super_values)
    
    pr_super = series_to_supervised(pr_scaled, n_lags)
    pr_super_values = pr_super.values  # to make just values
    print(pr_super_values)
    
    result = pd.concat([pr_super, Q_super1], axis=1)
    
    result=result.reset_index(inplace=False)
    result1=result.drop(['index'], axis = 1)#delete first two columns
    result1.columns
    
    X=result1.iloc[:,0:n_lags+1]
    Y=result1.iloc[:,n_lags+1:n_lags+2]
    
    train_X =X.iloc[:n_train, :]
    train_Y = Y.iloc[:n_train, :]
    
    test_X =X.iloc[n_train:, :]
    test_Y = Y.iloc[n_train:, :]
    
    print(np.shape(train_X), np.shape(train_Y),np.shape(test_X),np.shape(test_Y))
    
    train_X=train_X.values
    train_Y=train_Y.values
    test_X=test_X.values
    test_Y=test_Y.values
    
    # reshape 
    # 2D into 3D 
    train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1]))
    train_Y = train_Y.reshape((train_Y.shape[0], 1, train_Y.shape[1]))
    test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1]))
    test_Y = test_Y.reshape((test_Y.shape[0], 1, test_Y.shape[1]))
    print(np.shape(train_X), np.shape(train_Y), np.shape(test_X), np.shape(test_Y))
    return train_X, train_Y, test_X, test_Y, test_dates

def create_model(train_X, train_Y, test_X, test_Y):
    def rmse(y_true, y_pred):
        return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1))
    
    n_test = 60
    n_epochs = 1000
    n_batch = 360

    os.environ['PYTHONHASHSEED'] = '0'
    np.random.seed(42)
    rn.seed(12345)
    session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
    tf.set_random_seed(1234)
    sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
    K.set_session(sess)

    model = Sequential()
    model.add(LSTM(units=, activation=,
                   input_shape=(None, train_X.shape[2]), use_bias=True,
                   bias_regularizer=L1L2(l1=0.01, l2=0.01)))
    model.add(Dropout())
    model.add(Dense(activation='linear', units=1, use_bias=True))

    adam = keras.optimizers.Adam(lr=)
    sgd = keras.optimizers.SGD(lr=)
    rmsprop = keras.optimizers.RMSprop(lr=)
    
    choiceval = 
    if choiceval == 'adam':
        optim = adam
    elif choiceval == 'sgd':
        optim = sgd
    else:
        optim = rmsprop

    model.compile(loss=rmse, optimizer=optim)

    earlystop = keras.callbacks.EarlyStopping(monitor='loss', min_delta=0.00000001, patience=5, verbose=1, mode='auto')
    model.fit(train_X, train_Y, batch_size=n_batch, epochs=n_epochs, verbose=2, shuffle=False, callbacks=[earlystop])
    loss = model.evaluate(test_X, test_Y, batch_size=n_test, verbose=0)
    return {'loss': loss, 'status': STATUS_OK, 'model': model}

if __name__ == '__main__':
    n_lags = 12
     
    path = "D:/Binata/LSTM_23.5.2022/Code/"

    best_run, best_model = optim.minimize(model=create_model, data=data, algo=tpe.suggest, max_evals=100, trials=Trials())
    train_X, train_Y, test_X, test_Y, test_dates = data()
    print("Best performing model chosen hyper-parameters:")
    print(best_run)

It works fine without Optimization. After adding optimization, I am having an error as below. I am new in Coding. Can you help me please? Its urgent.

InvalidArgumentError: Expected multiples argument to be a vector of length 1 but got length 2 [[Node: training_7/SGD/gradients/loss_7/dense_9_loss/Mean_3_grad/Tile = Tile[T=DT_FLOAT, Tmultiples=DT_INT32, _class=["loc:@training_7/SGD/gradients/loss_7/dense_9_loss/Mean_3_grad/truediv"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](training_7/SGD/gradients/loss_7/dense_9_loss/Mean_3_grad/Reshape, training_7/SGD/gradients/loss_7/dense_9_loss/Mean_3_grad/Shape)]] [[Node: loss_7/add/_75 = _Recvclient_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_1654_loss_7/add", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]]



source https://stackoverflow.com/questions/72664212/invalidargumenterror-expected-multiples-argument-to-be-a-vector-of-length-1-but

Comments

Popular posts from this blog

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console

How to split a rinex file if I need 24 hours data

Trying to divide rinex file using the command gfzrnx but getting this error. While doing that getting this error msg 'gfzrnx' is not recognized as an internal or external command Trying to split rinex file using the command gfzrnx. also install'gfzrnx'. my doubt is I need to run this program in 'gfzrnx' or in 'cmdprompt'. I am expecting a rinex file with 24 hrs or 1 day data.I Have 48 hrs data in RINEX format. Please help me to solve this issue. source https://stackoverflow.com/questions/75385367/how-to-split-a-rinex-file-if-i-need-24-hours-data