【发布时间】:2017-08-31 03:10:51
【问题描述】:
我正在处理蛋白质序列。我的目标是创建一个卷积网络,它将预测蛋白质中每个氨基酸的三个角度。我在调试需要重塑操作的 TFLearn DNN 模型时遇到问题。
输入数据描述(当前)25 种不同长度的蛋白质。要使用张量,我需要有统一的尺寸,所以我用零填充空输入单元格。每个氨基酸由一个 4 维代码表示。除了帮助您理解张量的形状之外,这些细节可能并不重要。
DNN 的输出是六个数字,分别代表三个角度的正弦和余弦。为了创建有序对,DNN 图将 [..., 6] 张量重塑为 [..., 3, 2]。我的目标数据以相同的方式编码。我使用余弦距离计算损失。
我构建了一个非卷积 DNN,它显示出良好的初始学习行为,这与我将在此处发布的代码非常相似。但是该模型单独处理了三个相邻的氨基酸。我想将每个蛋白质视为一个单元——一开始滑动窗口宽度为 3 个氨基酸,最终更大。
现在我正在转换为卷积模型,我似乎无法让形状匹配。以下是我的代码的工作部分:
import tensorflow as tf
import tflearn as tfl
from protein import ProteinDatabase # don't worry about its details
def backbone_angle_distance(predict, actual):
with tf.name_scope("BackboneAngleDistance"):
actual = tfl.reshape(actual, [-1,3,2])
# Supply the -1 argument for axis that TFLearn can't pass
loss = tf.losses.cosine_distance(predict, actual, -1,
reduction=tf.losses.Reduction.MEAN)
return loss
# Training data
database = ProteinDatabase("./data")
inp, tgt = database.training_arrays()
# DNN model, convolution only in topmost layer for now
net = tfl.input_data(shape=[None, None, 4])
net = tfl.conv_1d(net, 24, 3)
net = tfl.conv_1d(net, 12, 1)
net = tfl.conv_1d(net, 6, 1)
net = tfl.reshape(net, [-1,3,2])
net = tf.nn.l2_normalize(net, dim=2)
net = tfl.regression(net, optimizer="sgd", learning_rate=0.1, \
loss=backbone_angle_distance)
model = tfl.DNN(net)
# Generate a prediction. Compare shapes for compatibility.
out = model.predict(inp)
print("\ninp : {}, shape = {}".format(type(inp), inp.shape))
print("out : {}, shape = {}".format(type(out), out.shape))
print("tgt : {}, shape = {}".format(type(tgt), tgt.shape))
print("tgt shape, if flattened by one dimension = {}\n".\
format(tgt.reshape([-1,3,2]).shape))
此时的输出为:
inp : <class 'numpy.ndarray'>, shape = (25, 543, 4)
out : <class 'numpy.ndarray'>, shape = (13575, 3, 2)
tgt : <class 'numpy.ndarray'>, shape = (25, 543, 3, 2)
tgt shape, if flattened by one dimension = (13575, 3, 2)
因此,如果我重塑 4D 张量 tgt,展平最外层维度,out 和 tgt 应该匹配。由于 TFLearn 的代码进行了批处理,因此我尝试在我的自定义损失函数骨干角距离()的第一行中截取并重塑 Tensor actual。
如果我添加几行来尝试模型拟合如下:
e, b = 1, 5
model.fit(inp, tgt, n_epoch=e, batch_size=b, validation_set=0.2, show_metric=True)
我得到以下额外的输出和错误:
---------------------------------
Run id: EEG6JW
Log directory: /tmp/tflearn_logs/
---------------------------------
Training samples: 20
Validation samples: 5
--
--
Traceback (most recent call last):
File "exp54.py", line 252, in <module>
model.fit(inp, tgt, n_epoch=e, batch_size=b, validation_set=0.2, show_metric=True)
File "/usr/local/lib/python3.5/dist-packages/tflearn/models/dnn.py", line 216, in fit
callbacks=callbacks)
File "/usr/local/lib/python3.5/dist-packages/tflearn/helpers/trainer.py", line 339, in fit
show_metric)
File "/usr/local/lib/python3.5/dist-packages/tflearn/helpers/trainer.py", line 818, in _train
feed_batch)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 789, in run
run_metadata_ptr)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 975, in _run
% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (5, 543, 3, 2) for Tensor 'TargetsData/Y:0', which has shape '(?, 3, 2)'
我在我的代码中的哪个位置指定 TargetsData/Y:0 的形状为 (?, 3, 2)?我知道不会。根据回溯,我实际上似乎从未在骨干角距离()中完成我的重塑操作。
感谢任何建议,谢谢!
【问题讨论】:
标签: python tensorflow tflearn