Python ValueError:检查输入时出错:预期gru_5_输入具有形状(无,无,10),但获得具有形状(1,4,1)的数组

Python ValueError:检查输入时出错:预期gru_5_输入具有形状(无,无,10),但获得具有形状(1,4,1)的数组,python,tensorflow,neural-network,keras,Python,Tensorflow,Neural Network,Keras,我正在尝试使用Python中使用TensorFlow和Keras的递归神经网络进行每小时的预测。我已将神经网络的输入指定为(无,无,5),如图所示 然而,我得到了一个错误 ValueError:检查输入时出错:预期gru_3_输入具有形状(无,无,10),但获得具有形状(1,4,1)的数组。My MVCE代码为ː 我不知道为什么会这样,但我相信这可能与重塑我的训练和测试数据有关。ɪ还将我的完整错误消息附加到我的代码中,以使问题重现 我不确定正确性,但这里是: %matplotlib inline

我正在尝试使用Python中使用TensorFlow和Keras的递归神经网络进行每小时的预测。我已将神经网络的输入指定为(无,无,5),如图所示

然而,我得到了一个错误

ValueError:检查输入时出错:预期gru_3_输入具有形状(无,无,10),但获得具有形状(1,4,1)的数组。
My MVCE代码为ː


我不知道为什么会这样,但我相信这可能与重塑我的训练和测试数据有关。ɪ还将我的完整错误消息附加到我的代码中,以使问题重现

我不确定正确性,但这里是:

%matplotlib inline
#!pip uninstall keras
#!pip install keras==2.1.2
import tensorflow as tf
import pandas as pd 
from pandas import DataFrame 
import math
import numpy 
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
import datetime
from keras.layers import Input, Dense, GRU, Embedding
from keras.optimizers import RMSprop
from keras.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard, ReduceLROnPlateau


datetime = [datetime.datetime(2012, 1, 1, 1, 0, 0) + datetime.timedelta(hours=i) for i in range(10)]




X=np.array([2.25226244,1.44078451,0.99174488,0.71179491,0.92824542,1.67776948,2.96399534,5.06257161,7.06504245,7.77817664
               ,0.92824542,1.67776948,2.96399534,5.06257161,7.06504245,7.77817664])

y= np.array([0.02062136,0.00186715,0.01517354,0.0129046 ,0.02231125,0.01492537,0.09646542,0.28444476,0.46289928,0.77817664
                ,0.02231125,0.01492537,0.09646542,0.28444476,0.46289928,0.77817664])

X = X[1:11]
y= y[1:11]

df = pd.DataFrame({'date':datetime,'y':y,'X':X})



df['t']= [x for x in range(10)]
df['X-1'] = df['X'].shift(-1)


x_data = df['X-1'].fillna(0)

y_data = y 

num_data = len(x_data) 

    #### training and testing split####
train_split = 0.6
num_train = int(train_split*num_data)   
num_test = num_data-num_train## number of observations in test set


    #input train test 
x_train = x_data[0:num_train].reshape(-1, 1)
x_test = x_data[num_train:].reshape(-1, 1)
    #print (len(x_train) +len( x_test))

    #output train test 
y_train = y_data[0:num_train].reshape(-1, 1)
y_test = y_data[num_train:].reshape(-1, 1)
    #print (len(y_train) + len(y_test))


    ### number of input signals 
num_x_signals = x_data.shape[0]
   # print (num_x_signals)

    ## number of output signals##

num_y_signals = y_data.shape[0]
    #print (num_y_signals)

    ####data scalling'###

x_scaler = MinMaxScaler(feature_range=(0,1))
x_train_scaled = x_scaler.fit_transform(x_train)



x_test_scaled = MinMaxScaler(feature_range=(0,1)).fit_transform(x_test)

y_scaler = MinMaxScaler()
y_train_scaled = y_scaler.fit_transform(y_train)
y_test_scaled = MinMaxScaler(feature_range=(0,1)).fit_transform(y_test)



def batch_generator(batch_size, sequence_length):
    """
    Generator function for creating random batches of training-data.
    """

    # Infinite loop.  providing the neural network with random data from the 
    # datase for x and y 
    while True:
        # Allocate a new array for the batch of input-signals.
        x_shape = (batch_size, sequence_length, num_x_signals)
        x_batch = np.zeros(shape=x_shape, dtype=np.float16)

        # Allocate a new array for the batch of output-signals.
        y_shape = (batch_size, sequence_length, num_y_signals)
        y_batch = np.zeros(shape=y_shape, dtype=np.float16)

        # Fill the batch with random sequences of data.
        for i in range(batch_size):
            # Get a random start-index.
            # This points somewhere into the training-data.
            idx = np.random.randint(num_train - sequence_length)

            # Copy the sequences of data starting at this index.
            x_batch[i] = x_train_scaled[idx:idx+sequence_length]
            y_batch[i] = y_train_scaled[idx:idx+sequence_length]

        yield (x_batch, y_batch)


batch_size =20




sequence_length = 2 


generator = batch_generator(batch_size=batch_size,
                            sequence_length=sequence_length)

x_batch, y_batch = next(generator)




#########Validation Set Start########

def batch_generator(batch_size, sequence_length):
    """
    Generator function for creating random batches of training-data.
    """

    # Infinite loop.  providing the neural network with random data from the 
    # datase for x and y 
    while True:
        # Allocate a new array for the batch of input-signals.
        x_shape = (batch_size, sequence_length, num_x_signals)
        x_batch = np.zeros(shape=x_shape, dtype=np.float16)

        # Allocate a new array for the batch of output-signals.
        y_shape = (batch_size, sequence_length, num_y_signals)
        y_batch = np.zeros(shape=y_shape, dtype=np.float16)

        # Fill the batch with random sequences of data.
        for i in range(batch_size):
            # Get a random start-index.
            # This points somewhere into the training-data.
            idx = np.random.randint(num_train - sequence_length)

            # Copy the sequences of data starting at this index.
            x_batch[i] = x_test_scaled[idx:idx+sequence_length]
            y_batch[i] = y_test_scaled[idx:idx+sequence_length]

        yield (x_batch, y_batch)
validation_data= next(batch_generator(batch_size,sequence_length))

# validation_data = (np.expand_dims(x_test_scaled, axis=0),
#                     np.expand_dims(y_test_scaled, axis=0))

#Validation set end

#####Create the Recurrent Neural Network###


model = Sequential()


model.add(GRU(units=5, 
                return_sequences=True,
                input_shape=(None, num_x_signals)))

## This line is going to map the above 512 values to just 1 (num_y_signal)
model.add(Dense(num_y_signals, activation='sigmoid'))

if False:
    from tensorflow.python.keras.initializers import RandomUniform

    # Maybe use lower init-ranges.##### I may have to change these during debugging####
    init = RandomUniform(minval=-0.05, maxval=0.05)

    model.add(Dense(num_y_signals,
                    activation='linear',
                    kernel_initializer=init))

warmup_steps = 5

def loss_mse_warmup(y_true, y_pred):

    #
    # Ignore the "warmup" parts of the sequences
    # by taking slices of the tensors.
    y_true_slice = y_true[:, warmup_steps:, :]
    y_pred_slice = y_pred[:, warmup_steps:, :]

    # These sliced tensors both have this shape:
    # [batch_size, sequence_length - warmup_steps, num_y_signals]

    # Calculate the MSE loss for each value in these tensors.
    # This outputs a 3-rank tensor of the same shape.
    loss = tf.losses.mean_squared_error(labels=y_true_slice,
                                        predictions=y_pred_slice)



    loss_mean = tf.reduce_mean(loss)

    return loss_mean

optimizer = RMSprop(lr=1e-3) ### This is somthing related to debugging 



model.compile(loss=loss_mse_warmup, optimizer=optimizer)#### I may have to make the output a singnal rather than the whole data set 

print(model.summary())


model.fit_generator(generator=generator,
                    epochs=20,
                    steps_per_epoch=100,
                    validation_data=validation_data)

我只在
验证集开始
验证集结束
之间更改了部分代码

验证数据的形状和一批培训生成器的形状不一样。
验证数据的形状是
(1,4,1)
,不能作为输入。@krishna,感谢您的回复,能否获得有关如何将我的验证设置为(无、无、10)的帮助由于我的印象是我已经完成了这个lineːmodel.add(GRU(units=5,return_sequences=True,input_shape=(None,num_x_signals)),所以您可以修改的训练生成器,并创建另一个用于生成验证数据。很抱歉,没有帮助。但是主要的想法是你必须使验证和训练数据的形状相同。我无法理解你代码的几个部分,这就是为什么我帮不了你。但我正在努力。@Khan11奇怪的训练和val的损失。@krishna非常感谢你们!这是目前为止运行的!。只是出于好奇,因为我刚刚接触Python和机器学习。我想问,培训和验证失败有什么奇怪的地方??谢谢,我们希望机器学习模型能学到一些东西。那么,我们如何衡量学习?我们通过测量损耗来实现。但学习并不意味着它应该只为训练集学习,它还应该在看不见的数据上表现良好。因此,验证损失是看不见的数据,我们在每个历元后检查模型的性能,看它是否在增加。一般来说,训练损失总是随着历元的增加而减少。但验证损失不是这样,有时它可能会保持停滞,有时它可能会增加,最好是在它增加时。好吧,这真的很有帮助!我的代码丢失功能和验证丢失当前为零:丢失:“0.0000e+00-val_丢失:0.0000e+00”。我认为这是错误的。这可能是因为我编写了错误地测量损失的代码吗?或者这可能是算法中的另一个缺陷?谢谢你帮我把这个分解!我没意识到这是直截了当的。我先前评论的最后一句话应该是“减少”。我建议你彻底了解背后的情况。不要急着去看花哨的结果。我也是初学者。英雄联盟
%matplotlib inline
#!pip uninstall keras
#!pip install keras==2.1.2
import tensorflow as tf
import pandas as pd 
from pandas import DataFrame 
import math
import numpy 
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
import datetime
from keras.layers import Input, Dense, GRU, Embedding
from keras.optimizers import RMSprop
from keras.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard, ReduceLROnPlateau


datetime = [datetime.datetime(2012, 1, 1, 1, 0, 0) + datetime.timedelta(hours=i) for i in range(10)]




X=np.array([2.25226244,1.44078451,0.99174488,0.71179491,0.92824542,1.67776948,2.96399534,5.06257161,7.06504245,7.77817664
               ,0.92824542,1.67776948,2.96399534,5.06257161,7.06504245,7.77817664])

y= np.array([0.02062136,0.00186715,0.01517354,0.0129046 ,0.02231125,0.01492537,0.09646542,0.28444476,0.46289928,0.77817664
                ,0.02231125,0.01492537,0.09646542,0.28444476,0.46289928,0.77817664])

X = X[1:11]
y= y[1:11]

df = pd.DataFrame({'date':datetime,'y':y,'X':X})



df['t']= [x for x in range(10)]
df['X-1'] = df['X'].shift(-1)


x_data = df['X-1'].fillna(0)

y_data = y 

num_data = len(x_data) 

    #### training and testing split####
train_split = 0.6
num_train = int(train_split*num_data)   
num_test = num_data-num_train## number of observations in test set


    #input train test 
x_train = x_data[0:num_train].reshape(-1, 1)
x_test = x_data[num_train:].reshape(-1, 1)
    #print (len(x_train) +len( x_test))

    #output train test 
y_train = y_data[0:num_train].reshape(-1, 1)
y_test = y_data[num_train:].reshape(-1, 1)
    #print (len(y_train) + len(y_test))


    ### number of input signals 
num_x_signals = x_data.shape[0]
   # print (num_x_signals)

    ## number of output signals##

num_y_signals = y_data.shape[0]
    #print (num_y_signals)

    ####data scalling'###

x_scaler = MinMaxScaler(feature_range=(0,1))
x_train_scaled = x_scaler.fit_transform(x_train)



x_test_scaled = MinMaxScaler(feature_range=(0,1)).fit_transform(x_test)

y_scaler = MinMaxScaler()
y_train_scaled = y_scaler.fit_transform(y_train)
y_test_scaled = MinMaxScaler(feature_range=(0,1)).fit_transform(y_test)



def batch_generator(batch_size, sequence_length):
    """
    Generator function for creating random batches of training-data.
    """

    # Infinite loop.  providing the neural network with random data from the 
    # datase for x and y 
    while True:
        # Allocate a new array for the batch of input-signals.
        x_shape = (batch_size, sequence_length, num_x_signals)
        x_batch = np.zeros(shape=x_shape, dtype=np.float16)

        # Allocate a new array for the batch of output-signals.
        y_shape = (batch_size, sequence_length, num_y_signals)
        y_batch = np.zeros(shape=y_shape, dtype=np.float16)

        # Fill the batch with random sequences of data.
        for i in range(batch_size):
            # Get a random start-index.
            # This points somewhere into the training-data.
            idx = np.random.randint(num_train - sequence_length)

            # Copy the sequences of data starting at this index.
            x_batch[i] = x_train_scaled[idx:idx+sequence_length]
            y_batch[i] = y_train_scaled[idx:idx+sequence_length]

        yield (x_batch, y_batch)


batch_size =20




sequence_length = 2 


generator = batch_generator(batch_size=batch_size,
                            sequence_length=sequence_length)

x_batch, y_batch = next(generator)




#########Validation Set Start########

def batch_generator(batch_size, sequence_length):
    """
    Generator function for creating random batches of training-data.
    """

    # Infinite loop.  providing the neural network with random data from the 
    # datase for x and y 
    while True:
        # Allocate a new array for the batch of input-signals.
        x_shape = (batch_size, sequence_length, num_x_signals)
        x_batch = np.zeros(shape=x_shape, dtype=np.float16)

        # Allocate a new array for the batch of output-signals.
        y_shape = (batch_size, sequence_length, num_y_signals)
        y_batch = np.zeros(shape=y_shape, dtype=np.float16)

        # Fill the batch with random sequences of data.
        for i in range(batch_size):
            # Get a random start-index.
            # This points somewhere into the training-data.
            idx = np.random.randint(num_train - sequence_length)

            # Copy the sequences of data starting at this index.
            x_batch[i] = x_test_scaled[idx:idx+sequence_length]
            y_batch[i] = y_test_scaled[idx:idx+sequence_length]

        yield (x_batch, y_batch)
validation_data= next(batch_generator(batch_size,sequence_length))

# validation_data = (np.expand_dims(x_test_scaled, axis=0),
#                     np.expand_dims(y_test_scaled, axis=0))

#Validation set end

#####Create the Recurrent Neural Network###


model = Sequential()


model.add(GRU(units=5, 
                return_sequences=True,
                input_shape=(None, num_x_signals)))

## This line is going to map the above 512 values to just 1 (num_y_signal)
model.add(Dense(num_y_signals, activation='sigmoid'))

if False:
    from tensorflow.python.keras.initializers import RandomUniform

    # Maybe use lower init-ranges.##### I may have to change these during debugging####
    init = RandomUniform(minval=-0.05, maxval=0.05)

    model.add(Dense(num_y_signals,
                    activation='linear',
                    kernel_initializer=init))

warmup_steps = 5

def loss_mse_warmup(y_true, y_pred):

    #
    # Ignore the "warmup" parts of the sequences
    # by taking slices of the tensors.
    y_true_slice = y_true[:, warmup_steps:, :]
    y_pred_slice = y_pred[:, warmup_steps:, :]

    # These sliced tensors both have this shape:
    # [batch_size, sequence_length - warmup_steps, num_y_signals]

    # Calculate the MSE loss for each value in these tensors.
    # This outputs a 3-rank tensor of the same shape.
    loss = tf.losses.mean_squared_error(labels=y_true_slice,
                                        predictions=y_pred_slice)



    loss_mean = tf.reduce_mean(loss)

    return loss_mean

optimizer = RMSprop(lr=1e-3) ### This is somthing related to debugging 



model.compile(loss=loss_mse_warmup, optimizer=optimizer)#### I may have to make the output a singnal rather than the whole data set 

print(model.summary())


model.fit_generator(generator=generator,
                    epochs=20,
                    steps_per_epoch=100,
                    validation_data=validation_data)