简体   繁体   English

在 TensorFlow/Keras 中将神经网络的子图视为模型

[英]Treating a SubGraph of a Neural Network as a Model In TensorFlow/Keras

I am trying to train an auto encoder in tensorflow using the Keras Layer API.我正在尝试使用 Keras Layer API 在 tensorflow 中训练自动编码器。 This API is quite nice and easy to use to setup the deep learning layers.这个 API 非常好,易于使用来设置深度学习层。

Just to review a quickly an autoencoder (in my mind) is a function $f(x) = z$ and its pseudo inverse \\hat{x} = f^{-1}(z) such that f(f^{-1}(x)) \\approx x.只是为了快速回顾一下自动编码器(在我看来)是一个函数 $f(x) = z$ 及其伪逆 \\hat{x} = f^{-1}(z) 使得 f(f^{- 1}(x)) \\大约 x。 In a neural network model, you would setup a neural network with a bottleneck layer that tries to predict itself x using f^{-1}(f(x)).在神经网络模型中,您将设置一个具有瓶颈层的神经网络,该神经网络尝试使用 f^{-1}(f(x)) 预测自身 x。 When the training error minimizes, you then have two components, z = f(x) is the prediction up until and including the bottleneck layer.当训练误差最小化时,您就有两个分量,z = f(x) 是直到并包括瓶颈层的预测。 f^{-1}(z) is the bottleneck layer to the end. f^{-1}(z) 是到最后的瓶颈层。

So I setup the encoder:所以我设置了编码器:

SZ = 6
model = tf.keras.Sequential()

model.add(layers.InputLayer(SZ))
model.add(layers.Dense(SZ))
model.add(layers.Dense(1))
model.add(layers.Dense(SZ))
model.summary()

model.compile('sgd','mse',metrics = ['accuracy'])
history= model.fit(returns.values,returns.values,epochs=100)

My difficulty here is that the weights and components (f being input+dense(SZ)+dense(1),f^{-1} being dense(1)+dense(SZ)) are trained but I do not know how to disentangle them.我的困难是权重和分量(f 是输入+密集(SZ)+密集(1),f^{-1} 是密集(1)+密集(SZ))被训练但我不知道如何解开它们。 Is there some way to break off the two layers in the neural network and have them treated as their own separate models?有没有办法将神经网络中的两层分开,并将它们视为自己独立的模型?

import tensorflow as tf
SZ=6
encoder_input = tf.keras.layers.Input(shape=(SZ,))
x = tf.keras.layers.Dense(SZ)(encoder_input)
x = tf.keras.layers.Dense(1)(x)
encoder_model = tf.keras.Model(inputs=encoder_input, outputs=x, name='encoder')

decoder_input = tf.keras.layers.Input(shape=(1,))
x2 = tf.keras.layers.Dense(SZ)(decoder_input)
decoder_model = tf.keras.Model(inputs=decoder_input, outputs=x2, name='decoder')

encoder_output = encoder_model(encoder_input)
decoder_output = decoder_model(encoder_output)

encoder_decoder_model = tf.keras.Model(inputs=encoder_input , outputs=decoder_output, name='encoder-decoder')
encoder_decoder_model.summary()

Here is the summary:这是摘要:

Model: "encoder-decoder"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_8 (InputLayer)         [(None, 6)]               0         
_________________________________________________________________
encoder (Model)              (None, 1)                 49        
_________________________________________________________________
decoder (Model)              (None, 6)                 12        
=================================================================
Total params: 61
Trainable params: 61
Non-trainable params: 0

you could train the encoder-decoder model and you separate encoder_model and decoder_model will be trained automatically.你可以训练的编码器,解码器模型和你分开encoder_modeldecoder_model将自动训练。 You could also retrieve them from your encoder_decoder model as follows:您还可以从您的 encoder_decoder 模型中检索它们,如下所示:

retrieved_encoder = encoder_decoder_model.get_layer('encoder')
retrieved_encoder.summary()

it prints:它打印:

Model: "encoder"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_8 (InputLayer)         [(None, 6)]               0         
_________________________________________________________________
dense_11 (Dense)             (None, 6)                 42        
_________________________________________________________________
dense_12 (Dense)             (None, 1)                 7         
=================================================================
Total params: 49
Trainable params: 49
Non-trainable params: 0

and the decoder:和解码器:

retrieved_decoder = encoder_decoder_model.get_layer('decoder')
retrieved_decoder.summary()

which prints:打印:

Model: "decoder"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_9 (InputLayer)         [(None, 1)]               0         
_________________________________________________________________
dense_13 (Dense)             (None, 6)                 12        
=================================================================
Total params: 12
Trainable params: 12
Non-trainable params: 0

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

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