在Keras模型中实现张量流图 [英] Implementing a tensorflow graph into a Keras model
问题描述
我正在尝试在Keras(最好是)或Tensorflow中实现以下大致体系结构.
I am trying to implement roughly the following architecture in Keras (preferably) or Tensorflow.
___________ _________ _________ ________ ______
| Conv | | Max | | Dense | | | | |
Input0--> | Layer 1 | --> | Pool 1 | --> | Layer | -->| | | |
|_________| |________| |________| | Sum | | Out |
| Layer |-->|_____|
Input1 ----------- Converted to trainable weights-->| |
|_______| |_______|
简而言之,它几乎是一个具有两个输入的模型,并使用Add([input0,input1])层合并为一个输出.诀窍是必须将输入之一视为变量=可训练的体重.
In short, it is pretty much a model with two inputs, merged into one output using an Add([input0, input1]) layer. The trick is that one of the inputs must be seen as a variable = trainable weight.
Keras层的Add()不允许这样做,并且它将input0和input1作为不可训练的变量:
Keras layer Add() does not allow this, and it takes input0 and input1 as non-trainable variables:
input0 = Input((28,28,1))
x = Conv2D(32, kernel_size=(3, 3), activation='relu',input_shape=input_shape)(mod1)
x = Conv2D(64, (3, 3), activation='relu')(input0)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Flatten()(x)
x = Dense(128, activation='relu')(x)
input1 = Input((128,))
x = Add()([x, input1])
x = Dense(num_classes, activation='softmax')(x)
model = Model(inputs = [mod1,TPM], outputs = x)
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
我可以在tensorflow中实现一个图,该图添加权重为b的占位符X,并学习相对于目标Y的b值.
I can implement a graph in tensorflow that adds a placeholder X with a weight b, and learns the value for b in respect to a target Y.
train_X = numpy.asarray([1.0, 2.0])
train_Y = numpy.asarray([0.0, 2.5])
n_samples = train_X.shape[0]
# tf Graph Input
X = tf.placeholder("float")
Y = tf.placeholder("float")
# Set model weights
b = tf.Variable([0.0, 0.0], name="bias")
# Construct a linear model
pred = tf.add(X, b)
loss = tf.reduce_mean(tf.square(pred - train_Y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
grads_and_vars = optimizer.compute_gradients(loss)
train = optimizer.apply_gradients(grads_and_vars)
#init = tf.initialize_all_variables()
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for step in range(epochs):
sess.run(train, feed_dict={X: train_X, Y: train_Y})
我想要的东西毫不夸张.简单优化输入和权重的添加.但是我不能将其包含在Keras模型中,我错过了融合两个想法的步骤.
Ths works exaclty how I want. Simple optimizable addition of an input and weights. But I can't include this into a Keras model.I am missing the step how to merge both ideas.
如何包含仅将一个可训练张量与一个不可训练张量相加的图层?
How can I include a layer that only sums one trainable tensor to a non-trainable tensor?
推荐答案
我不确定我是否完全了解您的需求.根据您的张量流代码,我认为您不必输入初始值.在这种情况下,我希望以下内容至少接近您想要的内容:
I'm not sure if I fully understand your needs. Based on your tensorflow code, I don't think you will have to feed in the initial value. In that case, I hope the following is at least close to what you want:
import numpy as np
import keras
from keras import backend as K
from keras.engine.topology import Layer
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense, Add
class MyLayer(Layer):
def __init__(self, bias_init, **kwargs):
self.bias_init = bias_init
super(MyLayer, self).__init__(**kwargs)
def build(self, input_shape):
self.bias = self.add_weight(name='bias',
shape=input_shape[1:],
initializer=keras.initializers.Constant(self.bias_init),
trainable=True)
super(MyLayer, self).build(input_shape) # Be sure to call this somewhere!
def call(self, x):
return x + self.bias
input0 = Input((28,28,1))
x = Conv2D(32, kernel_size=(3, 3), activation='relu',input_shape=(28,28,1))(input0)
x = Conv2D(64, (3, 3), activation='relu')(input0)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Flatten()(x)
x = Dense(128, activation='relu')(x)
input1 = np.random.rand(128)
x = MyLayer(input1)(x)
x = Dense(10, activation='softmax')(x)
model = Model(inputs=input0, outputs=x)
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
这篇关于在Keras模型中实现张量流图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!