【发布时间】:2016-05-05 22:51:12
【问题描述】:
我知道有人以各种形式提出过这个问题,但我真的找不到任何我能理解和使用的答案。如果这是一个基本问题,请原谅我,因为我是这些工具的新手(theano/keras)
要解决的问题
在神经网络中监控变量 (例如 LSTM 中的输入/遗忘/输出门值)
我目前得到了什么
无论我在哪个阶段获得这些值,我都会得到类似的东西:
Elemwise{mul,no_inplace}.0
Elemwise{mul,no_inplace}.0
[for{cpu,scan_fn}.2, Subtensor{int64::}.0, Subtensor{int64::}.0]
[for{cpu,scan_fn}.2, Subtensor{int64::}.0, Subtensor{int64::}.0]
Subtensor{int64}.0
Subtensor{int64}.0
有什么方法我无法监控(例如打印到标准输出、写入文件等)吗?
可能的解决方案
似乎 Keras 中的回调可以完成这项工作,但它对我也不起作用。我得到了和上面一样的东西
我的猜测
似乎我犯了非常简单的错误。
提前非常感谢大家。
添加
具体来说,我正在尝试监控 LSTM 中的输入/忘记/输出门控值。 我发现 LSTM.step() 用于计算这些值:
def step(self, x, states):
h_tm1 = states[0] # hidden state of the previous time step
c_tm1 = states[1] # cell state from the previous time step
B_U = states[2] # dropout matrices for recurrent units?
B_W = states[3] # dropout matrices for input units?
if self.consume_less == 'cpu': # just cut x into 4 pieces in columns
x_i = x[:, :self.output_dim]
x_f = x[:, self.output_dim: 2 * self.output_dim]
x_c = x[:, 2 * self.output_dim: 3 * self.output_dim]
x_o = x[:, 3 * self.output_dim:]
else:
x_i = K.dot(x * B_W[0], self.W_i) + self.b_i
x_f = K.dot(x * B_W[1], self.W_f) + self.b_f
x_c = K.dot(x * B_W[2], self.W_c) + self.b_c
x_o = K.dot(x * B_W[3], self.W_o) + self.b_o
i = self.inner_activation(x_i + K.dot(h_tm1 * B_U[0], self.U_i))
f = self.inner_activation(x_f + K.dot(h_tm1 * B_U[1], self.U_f))
c = f * c_tm1 + i * self.activation(x_c + K.dot(h_tm1 * B_U[2], self.U_c))
o = self.inner_activation(x_o + K.dot(h_tm1 * B_U[3], self.U_o))
with open("test_visualization.txt", "a") as myfile:
myfile.write(str(i)+"\n")
h = o * self.activation(c)
return h, [h, c]
因为它在上面的代码中,我试图将 i 的值写入一个文件,但它只给了我这样的值:
Elemwise{mul,no_inplace}.0
[for{cpu,scan_fn}.2, Subtensor{int64::}.0, Subtensor{int64::}.0]
Subtensor{int64}.0
所以我尝试了 i.eval() 或 i.get_value(),但都没有给我值。
.eval() 给了我这个:
theano.gof.fg.MissingInputError: An input of the graph, used to compute Subtensor{::, :int64:}(<TensorType(float32, matrix)>, Constant{10}), was not provided and not given a value.Use the Theano flag exception_verbosity='high',for more information on this error.
和 .get_value() 给了我这个:
AttributeError: 'TensorVariable' object has no attribute 'get_value'
所以我回溯了那些链(哪一行调用了哪些函数..),并试图在我找到的每一步获取值,但徒劳无功。
感觉我陷入了一些基本的陷阱。
【问题讨论】:
-
您是如何获得这些值的?包括您的代码,似乎您正在打印符号变量而不是它们的值。
-
非常感谢您的快速回复@MatiasValdenegro。我用代码和错误消息更新了上面的问题。
标签: callback monitoring theano keras