tot*_*uta 7 monitoring callback theano keras
我知道这个问题已经以各种形式提出,但我找不到任何我能理解和使用的答案.如果这是一个基本问题,请原谅我,因为我是这些工具的新手(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
Run Code Online (Sandbox Code Playgroud)
有什么方法我无法监控(例如打印到stdout,写入文件等)吗?
可能解决方案
似乎像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]
Run Code Online (Sandbox Code Playgroud)
正如在上面的代码中,我试图将i的值写入文件,但它只给了我以下值:
Elemwise{mul,no_inplace}.0
[for{cpu,scan_fn}.2, Subtensor{int64::}.0, Subtensor{int64::}.0]
Subtensor{int64}.0
Run Code Online (Sandbox Code Playgroud)
所以我尝试了itemsval()或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.
Run Code Online (Sandbox Code Playgroud)
和.get_value()给了我这个:
AttributeError: 'TensorVariable' object has no attribute 'get_value'
Run Code Online (Sandbox Code Playgroud)
所以我回溯了那些链(哪些行调用哪个函数..)并试图在我找到的每个步骤中获取值但是徒劳无功.
感觉就像我在一些基本的陷阱.
我使用 Keras FAQ 中描述的解决方案:
http://keras.io/getting-started/faq/#how-can-i-visualize-the-output-of-an-intermediate-layer
详细地:
from keras import backend as K
intermediate_tensor_function = K.function([model.layers[0].input],[model.layers[layer_of_interest].output])
intermediate_tensor = intermediate_tensor_function([thisInput])[0]
Run Code Online (Sandbox Code Playgroud)
产量:
array([[ 3., 17.]], dtype=float32)
Run Code Online (Sandbox Code Playgroud)
不过,我想使用函数式 API,但我似乎无法获得实际的张量,只能获得符号表示。例如:
model.layers[1].output
Run Code Online (Sandbox Code Playgroud)
产量:
<tf.Tensor 'add:0' shape=(?, 2) dtype=float32>
Run Code Online (Sandbox Code Playgroud)
我在这里遗漏了一些有关 Keras 和 Tensorflow 交互的信息,但我不确定是什么。任何见解都非常感激。
| 归档时间: |
|
| 查看次数: |
5138 次 |
| 最近记录: |