简体   繁体   English

python 3 中 tensorflow 中的多输出和多损失函数的问题

[英]Problem with multi-output and multi-loss functions in tensorflow in python 3

I want to train LSTM autoencoder.我想训练 LSTM 自动编码器。 I have N observations with 2 features, one of them is continuous and one is binary: I want to predict continuous by linear + MAE loss and binary by sigmoid + BCE loss.我有 N 个具有 2 个特征的观测值,其中一个是连续的,一个是二元的:我想通过线性 + MAE 损失预测连续,通过 sigmoid + BCE 损失预测二元。 In preprocessing phase I have created sequences, as sliding window of 3 observations.在预处理阶段,我创建了序列,作为滑动 window 的 3 个观察值。 This is how the model looks like这就是 model 的样子

input_ae = Input(shape=(X_train.shape[1], X_train.shape[2]))
print(input_ae.shape)
LSTM1 = keras.layers.LSTM(units=6, return_sequences=True, activation = 'selu')(input_ae)
LSTM2 = keras.layers.LSTM(units=1, activation= 'tanh')(LSTM1)
bottleneck = keras.layers.RepeatVector(n=X_train.shape[1])(LSTM2) # bottleneck layer
LSTM3 = keras.layers.LSTM(units=1, return_sequences=True)(bottleneck)
LSTM4 = keras.layers.LSTM(units=6, return_sequences=True, activation = 'selu')(LSTM3)

out1 = keras.layers.TimeDistributed(
    keras.layers.Dense(units=1, activation = 'linear')
  )(LSTM4)

out2 = keras.layers.TimeDistributed(
    keras.layers.Dense(units=1, activation = 'sigmoid')
  )(LSTM4)

model = Model(input_ae, outputs=[out1, out2]) 
model.compile(optimizer='adam', loss=["mae","binary_crossentropy"],
    loss_weights=[1.0, 1.0],) # this is trainable
history = model.fit(X_train, [X_train[:,:,0,np.newaxis], X_train[:,:,1,np.newaxis]],
                epochs=50,
                batch_size=3,
                shuffle=False) # observations are sorted by date !

这就是模型的样子

However I am still getting the following errors regarding topology of the graph of my model.但是,关于 model 的图形拓扑,我仍然收到以下错误。 I am new to python and now I do not know whether my coding is wrong or there is some conceptual mistake.我是 python 的新手,现在我不知道我的编码是否错误或存在一些概念错误。 Predicted values of variable2 (binary) are still around 0.5 (between 0.4 and 0.6) and they are not pushed towards 0 or 1.变量 2(二进制)的预测值仍然在 0.5 左右(介于 0.4 和 0.6 之间)并且它们不会被推向 0 或 1。

2020-05-25 13:44:33.862501: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2020-05-25 13:44:33.882555: I tensorflow/core/common_runtime/process_util.cc:147] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
Epoch 1/50
2020-05-25 13:44:39.191662: E tensorflow/core/grappler/optimizers/meta_optimizer.cc:561] remapper failed: Invalid argument: MutableGraphView::MutableGraphView error: node 'loss/time_distributed_2_loss/binary_crossentropy/weighted_loss/concat' has self cycle fanin 'loss/time_distributed_2_loss/binary_crossentropy/weighted_loss/concat'.
2020-05-25 13:44:39.229395: E tensorflow/core/grappler/optimizers/dependency_optimizer.cc:717] Iteration = 0, topological sort failed with message: The graph couldn't be sorted in topological order.
2020-05-25 13:44:39.247559: E tensorflow/core/grappler/optimizers/dependency_optimizer.cc:717] Iteration = 1, topological sort failed with message: The graph couldn't be sorted in topological order.
2020-05-25 13:44:39.345620: E tensorflow/core/grappler/optimizers/meta_optimizer.cc:561] arithmetic_optimizer failed: Invalid argument: The graph couldn't be sorted in topological order.
2020-05-25 13:44:39.363350: E tensorflow/core/grappler/optimizers/meta_optimizer.cc:561] remapper failed: Invalid argument: MutableGraphView::MutableGraphView error: node 'loss/time_distributed_2_loss/binary_crossentropy/weighted_loss/concat' has self cycle fanin 'loss/time_distributed_2_loss/binary_crossentropy/weighted_loss/concat'.
2020-05-25 13:44:39.383818: E tensorflow/core/grappler/optimizers/dependency_optimizer.cc:717] Iteration = 0, topological sort failed with message: The graph couldn't be sorted in topological order.
2020-05-25 13:44:39.399577: E tensorflow/core/grappler/optimizers/dependency_optimizer.cc:717] Iteration = 1, topological sort failed with message: The graph couldn't be sorted in topological order.
2020-05-25 13:44:39.432358: W tensorflow/core/common_runtime/process_function_library_runtime.cc:697] Ignoring multi-device function optimization failure: Invalid argument: The graph couldn't be sorted in topological order.
36/36 [==============================] - 3s 82ms/step - loss: 0.9305 - time_distributed_1_loss: 0.2378 - time_distributed_2_loss: 0.6927
Epoch 2/50
36/36 [==============================] - 0s 2ms/step - loss: 0.8997 - time_distributed_1_loss: 0.2085 - time_distributed_2_loss: 0.6912

The error is each time only in first epoch, later not.错误每次仅在第一个时期,以后不是。

That's not an error, but it's definitely annoying.这不是错误,但肯定很烦人。 You can silence this with this line of code before your Tensorflow import :您可以在 Tensorflow 导入之前使用这行代码将其静音:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

from tensorflow import keras # etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM