[英]tf.keras: feeding bool type variable along the input in model.fit method
I am building a model and I need to pass a bool type variable with True value with model.fit method and with False value with model.predict because I want need to run a condition(tf.condition or switch) function that works differently when the conditional bool variable is true or false.我正在构建一个模型,我需要使用 model.fit 方法传递一个带有 True 值的 bool 类型变量,并使用 model.predict 传递一个 False 值,因为我需要运行一个条件(tf.condition 或 switch)函数,当条件布尔变量为真或假。 When I tried to feed it along the input, it gives me error with validation split as input shape is (89084,127) while bool is just a bool value.当我尝试沿着输入输入它时,它给我带来了验证拆分错误,因为输入形状是 (89084,127) 而 bool 只是一个 bool 值。 Can anyone suggest me how to pass this bool variable to the model?谁能建议我如何将这个 bool 变量传递给模型?
My code is here:我的代码在这里:
Model模型
######### \no return sequences
import tensorflow.keras.backend as K
global flag
flag=True
input_size=127
dummy=np.zeros((1,19),dtype=np.float64)
dummy=tf.convert_to_tensor(dummy)
embedding_size=100
lstm_size=128
learn_rate=0.01
drop_out=0.1
output_size=19
condition = Input( shape=[], dtype=bool,name = "condition")
#condition=K.placeholder(shape=(1,),dtype=tf.bool)
#----------------------------Model for current utterance--------------------------------
current_input=Input(shape=(input_size,))
emb_current = Embedding(vocab_size, embedding_size,weights=[embedding_matrix], input_length=input_size, name='current_embed',trainable=False)(current_input)
out_current_b1=Bidirectional(LSTM(units=lstm_size, return_sequences=True))(emb_current )
attention_vector_current=attention(return_sequences=False)(out_current_b1)
out_current = Reshape((1,attention_vector_current.shape[1]))(attention_vector_current)
context1_input_da=Input(shape=(output_size,))
#-----------------------------Combined Model-------------------------------------------
def true_fn():
prev_DA=Reshape((1,output_size))(context1_input_da)
combined_train= Concatenate(axis=-1,name='true_train')([prev_DA, out_current])
return combined_train
def false_fn():
combined_without_previous = Concatenate(axis=-1,name='without_context')([out_current])
hidden_without_previous =LSTM(units=lstm_size,name="lstm_test")(combined_without_previous)
output_without_previous = Dense(units=19, activation='softmax')(hidden_without_previous)
# Slice the output_without_previous
previous_scores = Concatenate(axis=-2, name='with_dummy_scores')([dummy, output_without_previous[:-1]])
previous_scores= tf.expand_dims(previous_scores, 1)
print('previous',previous_scores.shape)
combined_test= Concatenate(axis=-1,name='False_test')([previous_scores, out_current])
return combined_test
combined = K.switch(condition, true_fn, false_fn)
hidden=LSTM(units=lstm_size)(combined)
output = Dense(units=len(unique_tag_set), activation='softmax')(hidden)
model = Model(inputs=[current_input,context1_input_da], outputs=output)
opt = Adam(lr=learn_rate)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
Fit method:拟合方法:
is_freeze= K.placeholder(shape=None,dtype=tf.bool)
is_freeze=True
history=model.fit([utt_minus_one,utt,prev_da_train], y_train,
epochs=1,batch_size=256,
shuffle = True, verbose = 1,
validation_split=0.2,
class_weight=custom_weight_dict)
How can I access this is_freeze bool variable in model or how can I pass it within fit method?如何在模型中访问这个is_freeze bool 变量,或者如何在 fit 方法中传递它?
It's not really the actual answer to your question but a more suitable way to solve the problem.这并不是您问题的真正答案,而是一种更合适的解决问题的方法。
The problem about the method you asked is that you already make the model by the time you train but you need to make a new model with different structure as you change the way you make the layer entirely.您问的方法的问题在于,您在训练时已经制作了模型,但是当您完全改变制作图层的方式时,您需要制作具有不同结构的新模型。
The trick is you don't need to make one model to do everything.诀窍是你不需要制作一个模型来做所有事情。 You can use the same layers to build 2 or 3 models and they'll all share the same weights.您可以使用相同的层来构建 2 或 3 个模型,它们都将共享相同的权重。
train_hidden=LSTM(units=lstm_size)(true_fn())
train_output = Dense(units=len(unique_tag_set), activation='softmax')(hidden)
inference_hidden=LSTM(units=lstm_size)(false_fn())
inference_output = Dense(units=len(unique_tag_set), activation='softmax')(hidden)
train_model = Model(inputs=[current_input,context1_input_da], outputs=train_output )
inference_model = Model(inputs=[current_input,context1_input_da], outputs=inference_output )
opt = Adam(lr=learn_rate)
train_model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
train_model.fit(x,y)
inference_model.predict(x)
The problem is your false_fn()
has these layers问题是你的false_fn()
有这些层
combined_without_previous = Concatenate(axis=-1,name='without_context')([out_current])
hidden_without_previous =LSTM(units=lstm_size,name="lstm_test")(combined_without_previous)
output_without_previous = Dense(units=19, activation='softmax')(hidden_without_previous)
Which will not get trained as you don't use this function when fit
so there's no way you can use this model build with false_fn()
to do anything unless you train it first.这不会得到训练,因为你在fit
时候不使用这个函数,所以除非你先训练它,否则你无法使用这个带有false_fn()
模型构建来做任何事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.