如何在Keras模型的末尾添加tensorflow数学函数?

时间:2019-12-24 13:15:56

标签: python tensorflow keras deep-learning

我试图在Keras模型的末尾添加简单的tensorflow数学函数,但是它不起作用。这是我使用本地Keras Add()函数的可笑但最少的工作代码:

import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as ss

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv1D, Add
import tensorflow as tf

kernel_size = 64
epochs = 1000

## Data generation for training

x_train = np.random.randn(1024, 512)

t = np.linspace(0, x_train.shape[1], x_train.shape[1], endpoint=False)
sine = np.sin(2*np.pi*t/32)
cosine = np.cos(2*np.pi*t/32)

x_I = np.multiply(x_train, cosine)
x_Q = np.multiply(x_train, sine)

b_I = ss.tukey(kernel_size)
b_Q = ss.tukey(kernel_size)

x_I_filt = np.array([np.convolve(b_I, x_I_i, mode='valid') for x_I_i in x_I])
x_Q_filt = np.array([np.convolve(b_Q, x_Q_i, mode='valid') for x_Q_i in x_Q])

y_train = x_Q_filt + x_I_filt

x_I = np.expand_dims(x_I, axis=2)
x_Q = np.expand_dims(x_Q, axis=2)
y_train = np.expand_dims(y_train, axis=2)

## Keras model

input_I = Input(shape=(x_I.shape[1], 1))
input_Q = Input(shape=(x_Q.shape[1], 1))

conv_I_1D = Conv1D(filters=1, kernel_size=kernel_size, activation=None, padding='valid', use_bias=False)(input_I)
conv_Q_1D = Conv1D(filters=1, kernel_size=kernel_size, activation=None, padding='valid', use_bias=False)(input_Q)

out_I_Q = Add()([conv_I_1D, conv_Q_1D]) 
# out_I_Q = tf.math.add(conv_I_1D, conv_I_1D)

model_1D = Model([input_I, input_Q], out_I_Q)

model_1D.compile(optimizer='sgd', loss='mean_squared_error') 
history_1D = model_1D.fit([x_I, x_Q], y_train, epochs=epochs, verbose=0)

40个时期之后,我的初始过滤器内核几乎完美了:

plt.semilogy(history_1D.history['loss'])
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.show()

enter image description here

但是,如果我用张量流等效添加函数Add()替换Keras out_I_Q = tf.math.add(conv_I_1D, conv_I_1D)函数,我将得到这个可悲的损失图: enter image description here 我认为在此配置中,tensorflow数学函数不是Keras模型的一部分。更改优化程序类型完全没有帮助。我正在使用tensorflow 2.0和Keras 2.2.5。

1 个答案:

答案 0 :(得分:1)

您应将tf.keras.layers.Lambda if(request.body) { //Switch statement } layer结合使用,如下所示:

tf.math.add()

def add_func(inputs):
    return tf.math.add(inputs[0], inputs[1])

out_I_Q = Lambda(add_func)([conv_I_1D, conv_Q_1D])

来自文档:

  

存在Lambda层,因此可以使用任意TensorFlow函数   在构建顺序和功能API模型时使用。

完整示例:

out_I_Q = Lambda(lambda x: tf.math.add(x[0], x[1]))([conv_I_1D, conv_Q_1D])

退出:

enter image description here