我有一个训练有素的深层神经网络,有一个句子级注意力层。该网络被称为GRU,如下所示,我想在测试后得到关注值(sen_alpha)的结果。
class GRU:
def __init__(self,is_training,word_embeddings,settings):
self.big_num = big_num = settings.big_num
for i in range(big_num):
sen_repre.append(tf.tanh(attention_r[self.total_shape[i]:self.total_shape[i+1]]))
batch_size = self.total_shape[i+1]-self.total_shape[i]
sen_alpha.append(tf.reshape(tf.nn.softmax(tf.reshape(tf.matmul(tf.mul(sen_repre[i],sen_a),sen_r),[batch_size])),[1,batch_size]))
self.attentions.append(sen_alpha[i])
测试代码:
def main(_):
test_settings = Settings()
with tf.Graph().as_default():
sess = tf.Session()
with sess.as_default():
with tf.variable_scope("model"):
mtest = GRU(is_training=False, word_embeddings = None, settings = test_settings)
saver = tf.train.Saver()
attentions = mtest.attentions
att = np.array(attentions)
print(str(type(att)))
print(att[0:100])
np.save("attentions.npy",att)
结果:
输入:输入'numpy.ndarray'
att [0:100]:
[<tf.Tensor 'model/Reshape_9:0' shape=(1, ?) dtype=float32<tf.Tensor 'model/Reshape_17:0' shape=(1, ?) dtype=float32<tf.Tensor 'model/Reshape_25:0' shape=(1, ?) dtype=float32>
错误:
文件“test_GRU.py”,第242行,在main中 np.save( “attentions.npy”,ATT)
cPickle.PicklingError:无法挑剔:属性查找内置。模块失败
如何正确保存结果?感谢
答案 0 :(得分:0)
我无法修复您的代码,但我可以为您提供从模型定义到提取值的逐步设计的简短版本:
sess = tf.Session()
。sess.run(tf.global_variables_initializer())
sess.run(the_tensor, dictionary_of_numpy_array_as_input_to_graph)
。 输出将是numpy数组,您可以保存它们。