您好,登錄后才能下訂單哦!
小編給大家分享一下如何解決keras使用cov1D函數的輸入問題,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討方法吧!
解決了以下錯誤:
1.ValueError: Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=4
2.ValueError: Error when checking target: expected dense_3 to have 3 dimensions, but got array with …
1.ValueError: Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=4
錯誤代碼:
model.add(Conv1D(8, kernel_size=3, strides=1, padding='same', input_shape=(x_train.shape))
或者
model.add(Conv1D(8, kernel_size=3, strides=1, padding='same', input_shape=(x_train.shape[1:])))
這是因為模型輸入的維數有誤,在使用基于tensorflow的keras中,cov1d的input_shape是二維的,應該:
1、reshape x_train的形狀
x_train=x_train.reshape((x_train.shape[0],x_train.shape[1],1))
x_test = x_test.reshape((x_test.shape[0], x_test.shape[1],1))
2、改變input_shape
model = Sequential()
model.add(Conv1D(8, kernel_size=3, strides=1, padding='same', input_shape=(x_train.shape[1],1)))
大神原文:
The input shape is wrong, it should be input_shape = (1, 3253) for Theano or (3253, 1) for TensorFlow. The input shape doesn't include the number of samples.
Then you need to reshape your data to include the channels axis:
x_train = x_train.reshape((500000, 1, 3253))
Or move the channels dimension to the end if you use TensorFlow. After these changes it should work.
2.ValueError: Error when checking target: expected dense_3 to have 3 dimensions, but got array with …
出現此問題是因為ylabel的維數與x_train x_test不符,既然將x_train x_test都reshape了,那么也需要對y進行reshape。
解決辦法:
同時對照x_train改變ylabel的形狀
t_train=t_train.reshape((t_train.shape[0],1))
t_test = t_test.reshape((t_test.shape[0],1))
附:
修改完的代碼:
import warnings warnings.filterwarnings("ignore") import os os.environ["CUDA_VISIBLE_DEVICES"] = "0" import pandas as pd import numpy as np import matplotlib # matplotlib.use('Agg') import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn import preprocessing from keras.models import Sequential from keras.layers import Dense, Dropout, BatchNormalization, Activation, Flatten, Conv1D from keras.callbacks import LearningRateScheduler, EarlyStopping, ModelCheckpoint, ReduceLROnPlateau from keras import optimizers from keras.regularizers import l2 from keras.models import load_model df_train = pd.read_csv('./input/train_V2.csv') df_test = pd.read_csv('./input/test_V2.csv') df_train.drop(df_train.index[[2744604]],inplace=True)#去掉nan值 df_train["distance"] = df_train["rideDistance"]+df_train["walkDistance"]+df_train["swimDistance"] # df_train["healthpack"] = df_train["boosts"] + df_train["heals"] df_train["skill"] = df_train["headshotKills"]+df_train["roadKills"] df_test["distance"] = df_test["rideDistance"]+df_test["walkDistance"]+df_test["swimDistance"] # df_test["healthpack"] = df_test["boosts"] + df_test["heals"] df_test["skill"] = df_test["headshotKills"]+df_test["roadKills"] df_train_size = df_train.groupby(['matchId','groupId']).size().reset_index(name='group_size') df_test_size = df_test.groupby(['matchId','groupId']).size().reset_index(name='group_size') df_train_mean = df_train.groupby(['matchId','groupId']).mean().reset_index() df_test_mean = df_test.groupby(['matchId','groupId']).mean().reset_index() df_train = pd.merge(df_train, df_train_mean, suffixes=["", "_mean"], how='left', on=['matchId', 'groupId']) df_test = pd.merge(df_test, df_test_mean, suffixes=["", "_mean"], how='left', on=['matchId', 'groupId']) del df_train_mean del df_test_mean df_train = pd.merge(df_train, df_train_size, how='left', on=['matchId', 'groupId']) df_test = pd.merge(df_test, df_test_size, how='left', on=['matchId', 'groupId']) del df_train_size del df_test_size target = 'winPlacePerc' train_columns = list(df_test.columns) """ remove some columns """ train_columns.remove("Id") train_columns.remove("matchId") train_columns.remove("groupId") train_columns_new = [] for name in train_columns: if '_' in name: train_columns_new.append(name) train_columns = train_columns_new # print(train_columns) X = df_train[train_columns] Y = df_test[train_columns] T = df_train[target] del df_train x_train, x_test, t_train, t_test = train_test_split(X, T, test_size = 0.2, random_state = 1234) # scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1)).fit(x_train) scaler = preprocessing.QuantileTransformer().fit(x_train) x_train = scaler.transform(x_train) x_test = scaler.transform(x_test) Y = scaler.transform(Y) x_train=x_train.reshape((x_train.shape[0],x_train.shape[1],1)) x_test = x_test.reshape((x_test.shape[0], x_test.shape[1],1)) t_train=t_train.reshape((t_train.shape[0],1)) t_test = t_test.reshape((t_test.shape[0],1)) model = Sequential() model.add(Conv1D(8, kernel_size=3, strides=1, padding='same', input_shape=(x_train.shape[1],1))) model.add(BatchNormalization()) model.add(Conv1D(8, kernel_size=3, strides=1, padding='same')) model.add(Conv1D(16, kernel_size=3, strides=1, padding='valid')) model.add(BatchNormalization()) model.add(Conv1D(16, kernel_size=3, strides=1, padding='same')) model.add(Conv1D(32, kernel_size=3, strides=1, padding='valid')) model.add(BatchNormalization()) model.add(Conv1D(32, kernel_size=3, strides=1, padding='same')) model.add(Conv1D(32, kernel_size=3, strides=1, padding='same')) model.add(Conv1D(64, kernel_size=3, strides=1, padding='same')) model.add(Activation('tanh')) model.add(Flatten()) model.add(Dropout(0.5)) # model.add(Dropout(0.25)) model.add(Dense(512,kernel_initializer='he_normal', activation='relu', W_regularizer=l2(0.01))) model.add(Dense(128,kernel_initializer='he_normal', activation='relu', W_regularizer=l2(0.01))) model.add(Dense(1, kernel_initializer='normal', activation='sigmoid')) optimizers.Adam(lr=0.01, epsilon=1e-8, decay=1e-4) model.compile(optimizer=optimizer, loss='mse', metrics=['mae']) model.summary() ng = EarlyStopping(monitor='val_mean_absolute_error', mode='min', patience=4, verbose=1) # model_checkpoint = ModelCheckpoint(filepath='best_model.h6', monitor='val_mean_absolute_error', mode = 'min', save_best_only=True, verbose=1) # reduce_lr = ReduceLROnPlateau(monitor='val_mean_absolute_error', mode = 'min',factor=0.5, patience=3, min_lr=0.0001, verbose=1) history = model.fit(x_train, t_train, validation_data=(x_test, t_test), epochs=30, batch_size=32768, callbacks=[early_stopping], verbose=1)predict(Y) pred = pred.ravel()
補充知識:Keras Conv1d 參數及輸入輸出詳解
Conv1d(in_channels,out_channels,kernel_size,stride=1,padding=0,dilation=1,groups=1,bias=True)
filters:卷積核的數目(即輸出的維度)
kernel_size: 整數或由單個整數構成的list/tuple,卷積核的空域或時域窗長度
strides: 整數或由單個整數構成的list/tuple,為卷積的步長。任何不為1的strides均為任何不為1的dilation_rata均不兼容
padding: 補0策略,為”valid”,”same”或”casual”,”casual”將產生因果(膨脹的)卷積,即output[t]不依賴于input[t+1:]。當對不能違反事件順序的時序信號建模時有用。“valid”代表只進行有效的卷積,即對邊界數據不處理。“same”代表保留邊界處的卷積結果,通常會導致輸出shape與輸入shape相同。
activation:激活函數,為預定義的激活函數名,或逐元素的Theano函數。如果不指定該函數,將不會使用任何激活函數(即使用線性激活函數:a(x)=x)
model.add(Conv1D(filters=nn_params["input_filters"], kernel_size=nn_params["filter_length"], strides=1, padding='valid', activation=nn_params["activation"], kernel_regularizer=l2(nn_params["reg"])))
例:輸入維度為(None,1000,4)
第一維度:None
第二維度:output_length = int((input_length - nn_params["filter_length"] + 1))
在此情況下為:output_length = (1000 + 2*padding - filters +1)/ strides = (1000 + 2*0 -32 +1)/1 = 969
第三維度:filters
看完了這篇文章,相信你對如何解決keras使用cov1D函數的輸入問題有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。