代码之家  ›  专栏  ›  技术社区  ›  rra

低精度和警告:警告:张量流:模型是用形状构建的?我想改变什么

  •  0
  • rra  · 技术社区  · 5 年前

    我正在用Keras做一个循环神经网络。我的数据帧由22个特征(x)组成,y是0和1(二进制)。 下面是我的代码,它与epoch 10的精度也很低。此外,它给了我一个 形状误差 我不知道该怎么改进。有人能帮我理解它问的形状吗?这真的很令人困惑,它需要为输入层和隐藏层提供哪种形状???

    from keras.models import Sequential
    from keras.layers import Dense,Dropout,Flatten
    from sklearn.model_selection import StratifiedKFold
    import numpy as np
    # fix random seed for reproducibility
    seed = 7
    numpy.random.seed(seed)
    
    #Pandas dataframe csv
    # split into input (X) and output (Y) variables
    X = dataset.iloc[:, :-1].values
    Y = dataset.iloc[:, 22].values
    
    # split into 67% for train and 33% for test
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=seed)
    
    #Check the shapes
    print(X_train.shape)
    y_train.shape
    (590217, 22)
    (590217,)
    
    #Reshape it and check the shape
    x_train = X_train.reshape(-1, 1, 22)
    x_test  = X_test.reshape(-1, 1, 22)
    print(x_train.shape)
    print(x_test.shape)
    (590217, 1, 22)
    (290704, 1, 22)
    
    
    # Create the model
    model = Sequential()
    model.add(LSTM(1, activation='relu', input_shape=(25, 22),return_sequences=True))
    model.add(LSTM(300, activation='relu'))
    model.add(Flatten())
    model.add(Dense(128))
    model.add(Dense(1))
    model.compile(optimizer='adam', loss='mse',metrics=['accuracy'])
    
    # Fit the model
    history = model.fit(x_train, y_train, epochs=3, batch_size=10)
    
    Epoch 1/3
    WARNING:tensorflow:Model was constructed with shape (None, 25, 22) for input Tensor("lstm_6_input:0", shape=(None, 25, 22), dtype=float32), but it was called on an input with incompatible shape (None, 1, 22).
    WARNING:tensorflow:Model was constructed with shape (None, 25, 22) for input Tensor("lstm_6_input:0", shape=(None, 25, 22), dtype=float32), but it was called on an input with incompatible shape (None, 1, 22).
    59022/59022 [==============================] - 337s 6ms/step - loss: 0.1966 - accuracy: 0.1096
    Epoch 2/3
    59022/59022 [==============================] - 310s 5ms/step - loss: 0.1841 - accuracy: 0.1584
    Epoch 3/3
    59022/59022 [==============================] - 294s 5ms/step - loss: 0.1823 - accuracy: 0.1721
    
    
    

    #我增加了迭代次数并检查了准确性,但没有得到改善,我还需要更改什么

    0 回复  |  直到 5 年前
        1
  •  1
  •   bebbieyin    5 年前

    在模型的第一层中,您输入了input_shape=(25,22),但您正试图将其拟合到具有形状(1,22)的数据集中。 为了提高准确性,请在将数据拟合到模型中之前尝试对其进行洗牌。

    import random
    np.random.shuffle(x_train)
    np.random.shuffle(y_train)
    

    你也可以试试 二元交叉熵 用于损失函数而不是MSE,因为它专门用于两类问题。

    推荐文章