如何解决当batchsize> 8时,InvalidArgumentError如何在generator中正确使用batchsize?
当我以批处理大小大于1运行这些生成器(对于LSTM)时,出现以下错误:
InvalidArgumentError: Incompatible shapes: [8,30,10] vs. [8,10]
[[node sub (defined at ....py:72) ]] [Op:__inference_train_function_6469]
Function call stack:
train_function
# EDIT:
# solverd
# But now I get:
InvalidArgumentError: Incompatible shapes: [8] vs. [0]
[[node gradient_tape/mean_squared_error/weighted_loss/BroadcastGradientArgs (defined at ....py:76) ]] [Op:__inference_train_function_400935]
Function call stack:
train_function
#for this setup:
#Input shape is: (3,33)
model.add(LSTM( 32,input_shape= (3,33),return_sequences= True ))
model.add(Dropout( 0.3 ));
model.add(BatchNormalization())
model.add(LSTM( 16 ))
model.add(Dropout( 0.2 ));
model.add(BatchNormalization())
model.add(Dense( 6,activation= tanh ))
Model: "sequential_23"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_46 (LSTM) (None,3,32) 8448
_________________________________________________________________
dropout_34 (Dropout) (None,32) 0
_________________________________________________________________
batch_normalization_43 (Batc (None,32) 128
_________________________________________________________________
lstm_47 (LSTM) (None,16) 3136
_________________________________________________________________
dropout_35 (Dropout) (None,16) 0
_________________________________________________________________
batch_normalization_44 (Batc (None,16) 64
_________________________________________________________________
dense_23 (Dense) (None,6) 102
=================================================================
Total params: 11,878
Trainable params: 11,782
Non-trainable params: 96
“ MyGenerator_with_SampleWeights”使用(一维数组)作为每个样本的权重,而另一个则不使用。
如果您发现一些有助于提高这两个生成器效率的方法,我也感到非常高兴。
class MyGenerator_with_SampleWeights(tf.keras.utils.Sequence) :
def __init__(self,list_x,labels,batch_size,sample_weights=None) :
self.labels = labels
self.batch_size = batch_size
self.list_x = list_x
self.sample_weights = sample_weights
def __len__(self) :
return (np.ceil(len(self.list_x) / float(self.batch_size))).astype(np.int)
def __getitem__(self,idx) :
batch_x = self.list_x[idx * self.batch_size : (idx+1) * self.batch_size]
batch_y = self.labels[idx * self.batch_size : (idx+1) * self.batch_size]
batch_weight = self.sample_weights[idx * self.batch_size : (idx+1) * self.batch_size]
return np.array(batch_x),np.array(batch_y),np.array(batch_weight)
class MyGenerator(tf.keras.utils.Sequence) :
def __init__(self,batch_size) :
self.labels = labels
self.batch_size = batch_size
self.list_x = list_x
def __len__(self) :
return (np.ceil(len(self.list_x) / float(self.batch_size))).astype(np.int)
def __getitem__(self,idx) :
batch_x = self.list_x[idx * self.batch_size : (idx+1) * self.batch_size]
batch_y = self.labels[idx * self.batch_size : (idx+1) * self.batch_size]
return np.array(batch_x),np.array(batch_y)
这是一个最小的可重现样品: (还要从上方复制生成器)
import tensorflow as tf,numpy as np;
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout,LSTM,BatchNormalization
from tensorflow.keras.callbacks import TensorBoard,ModelCheckpoint
path= r"C:\Users\User\documents\somefolder" # <--- your path
model = Sequential()
model.add(LSTM(500,input_shape=X_shapes,return_sequences=True))
model.add(Dropout(0.1)); model.add(BatchNormalization())
model.add(LSTM(100))
model.add(Dropout(0.1)); model.add(BatchNormalization())
model.add(Dense(Y_cols,activation='tanh')); model.add(Dropout(0.2))
optimizer = tf.keras.optimizers.Adam(lr=0.005,decay=1e-7)
model.compile(loss='mean_squared_error',optimizer=optimizer,metrics=['mae'])
BATCH_SIZE = 8
X_shapes = (30,5)
EPOCHS = 3
Y_cols,len_X,len_Y = 10,1000,200
train_X,train_Y = np.asarray([np.random.random(X_shapes) for x in range(len_X)]),np.random.random((len_X,Y_cols))
validation_X,validation_Y = np.asarray([np.random.random(X_shapes) for x in range(len_Y)]),np.random.random((len_Y,Y_cols))
weight_arr = np.random.random((len_X,1))
tb = TensorBoard(path)
cp = ModelCheckpoint(path,monitor='val_loss',verbose=1,save_best_only=True,mode='min')
if weight_arr is None:
train_gen = MyGenerator(train_X,train_Y,BATCH_SIZE)
else:
train_gen = MyGenerator_with_SampleWeights(train_X,BATCH_SIZE,sample_weights=weight_arr)
validation_gen = MyGenerator(validation_X,validation_Y,BATCH_SIZE)
model.fit_generator(train_gen,steps_per_epoch=None,epochs=EPOCHS,validation_data = validation_gen,callbacks=[tb,cp])
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。