我尝试根据分别存储为json字符串和h5权重的预训练模型进行预测,但是,即使输入和模型完全相同,后端(Tensorflow和Theano)似乎也会给我带来不同的输出。我发现即使在第一层即一维卷积中,激活也不同,这是从convolution1D层的第5个滤镜打印部分激活的代码:
Theano版本:
from keras.models import model_from_json
import numpy as np
import os
os.environ['KERAS_BACKEND'] = 'theano'
model_file = 'model.h5'
x_file = 'x.csv'
model_json = '{"class_name": "Model", "keras_version": "1.2.2", "config": {"layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 1002, 6], "input_dtype": "float32", "sparse": false, "name": "input_1"}, "inbound_nodes": [], "name": "input_1"}, {"class_name": "Convolution1D", "config": {"batch_input_shape": [null, null, 6], "W_constraint": null, "b_constraint": null, "name": "convolution1d_1", "activity_regularizer": null, "trainable": true, "filter_length": 34, "init": "glorot_uniform", "bias": true, "nb_filter": 128, "input_dtype": "float32", "subsample_length": 1, "border_mode": "valid", "input_dim": 6, "b_regularizer": null, "W_regularizer": null, "activation": "relu", "input_length": null}, "inbound_nodes": [[["input_1", 0, 0]]], "name": "convolution1d_1"}], "input_layers": [["input_1", 0, 0]], "output_layers": [["convolution1d_1", 0, 0]], "name": "model_1"}}'
model = model_from_json(model_json)
model.load_weights(model_file)
x=np.loadtxt(x_file)
x = np.reshape(x,(1,x.shape[0],x.shape[1]))
y = model.predict(x)
y[0,range(230),4]
输入和输出看起来像: Theano version
Tensorflow版本:
from keras.models import model_from_json
import numpy as np
import os
os.environ['KERAS_BACKEND'] = 'tensorflow'
model_file = 'model.h5'
x_file = 'x.csv'
model_json = '{"class_name": "Model", "keras_version": "1.2.2", "config": {"layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 1002, 6], "input_dtype": "float32", "sparse": false, "name": "input_1"}, "inbound_nodes": [], "name": "input_1"}, {"class_name": "Convolution1D", "config": {"batch_input_shape": [null, null, 6], "W_constraint": null, "b_constraint": null, "name": "convolution1d_1", "activity_regularizer": null, "trainable": true, "filter_length": 34, "init": "glorot_uniform", "bias": true, "nb_filter": 128, "input_dtype": "float32", "subsample_length": 1, "border_mode": "valid", "input_dim": 6, "b_regularizer": null, "W_regularizer": null, "activation": "relu", "input_length": null}, "inbound_nodes": [[["input_1", 0, 0]]], "name": "convolution1d_1"}], "input_layers": [["input_1", 0, 0]], "output_layers": [["convolution1d_1", 0, 0]], "name": "model_1"}}'
model = model_from_json(model_json)
model.load_weights(model_file)
x=np.loadtxt(x_file)
x = np.reshape(x,(1,x.shape[0],x.shape[1]))
y = model.predict(x)
y[0,range(230),4]
输入和输出看起来像: Tensorflow version
经过几次实验,我发现Theano倾向于给出“错误”的答案,这是一个示例,用于计算第5个滤镜的第一个窗口(在该模型中,bias为零,我已经检查过了):
l=model.get_layer(index=1)
w1 = l.get_weights()[0]
w2 = l.get_weights()[1]
data1 = w1[:,0,:,4]
data2 = x[0,range(34),:]
ans=0
for i in range(6):
ans += np.sum(np.multiply(data1[:,i],data2[:,i]))
Ans等于0.08544020017143339 Tensorflow给出0.08544022,与根据我的计算应该是相同的,但是Theano给出了0.0518605。有人可以提出一个解释吗?
答案 0 :(得分:1)
在计算期间,看起来Theano的仁权重发生了变化。下面的代码清楚地显示了Tensorflow和Theano的区别:
import numpy as np
import os
os.environ['KERAS_BACKEND'] = 'theano'
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation,Convolution1D
from keras.optimizers import SGD
model = Sequential()
model.add(Convolution1D(1, 2, border_mode='valid', input_shape=(6, 1),bias=True,activation='relu'))
l=model.get_layer(index=1)
w1 = l.get_weights()[0]
w2 = l.get_weights()[1]
np.random.seed(0)
x = np.random.random((1,6,1))
y = model.predict(x)
a_tf = np.sum(np.multiply(w1[:,0,0,0],x[0,range(2),0])) # y[0,0] would equal to this with Tensorflow backend
a_th = np.sum(np.multiply(np.flip(w1[:,0,0,0]),x[0,range(2),0])) # y[0,0] would equal to this with Theano backend