简体   繁体   English

tf.keras:在 model.fit 方法中沿输入馈送 bool 类型变量

[英]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.

相关问题 梯度累积与自定义 model.fit TF.Keras? - Gradient Accumulation with Custom model.fit in TF.Keras? tf.keras (RNN) 层在运行 model.fit() 时出现问题 - tf.keras (RNN) Layer issues when running model.fit() 在 tf.keras 中的 model.fit 中,有没有办法将每个样本分批传递 n 次? - In model.fit in tf.keras, is there a way to pass each sample in a batch n times? 如何使用 tf.keras 拟合 LSTM 模型 - How to fit a LSTM model using tf.keras tf.keras.to_categorical TypeError 在 model.fit 期间 - tf.keras.to_categorical TypeError during model.fit tf.keras:在没有自定义训练方法的情况下处理自定义 Model 中可变长度序列的迭代 - tf.keras: Handling iteration over variable-length sequences in custom Model without custom training method 使用 tf.keras 保存模型 - Saving a model with tf.keras model.fit() 不接受 tf.data.Dataset 的输入形状 - Input shape of tf.data.Dataset not accepted by model.fit() Tensorflow (Keras API) `model.fit` 方法返回“Failed to convert object of type<class 'tuple'> 张量”错误</class> - Tensorflow (Keras API) `model.fit` method returns “Failed to convert object of type <class 'tuple'> to Tensor” error 带有 Model.Fit() 的 Keras InvalidArgumentError - Keras InvalidArgumentError With Model.Fit()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM