问题
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-->| |
|_______| |_______|
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 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'])
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})
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?
回答1:
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'])
来源:https://stackoverflow.com/questions/50120251/implementing-a-tensorflow-graph-into-a-keras-model