2017-04-12 64 views
1
相当于

我想实现CNTK DDPG和整个下面的代码(使用Tensorflow)创建评论家网络传来:什么是以下tensorflow片段的CNTK

state_input = tf.placeholder("float",[None,state_dim]) 
action_input = tf.placeholder("float",[None,action_dim]) 

W1 = self.variable([state_dim,layer1_size],state_dim) 
b1 = self.variable([layer1_size],state_dim) 
W2 = self.variable([layer1_size,layer2_size],layer1_size+action_dim) 
W2_action = self.variable([action_dim,layer2_size],layer1_size+action_dim) 
b2 = self.variable([layer2_size],layer1_size+action_dim) 
W3 = tf.Variable(tf.random_uniform([layer2_size,1],-3e-3,3e-3)) 
b3 = tf.Variable(tf.random_uniform([1],-3e-3,3e-3)) 

layer1 = tf.nn.relu(tf.matmul(state_input,W1) + b1) 
layer2 = tf.nn.relu(tf.matmul(layer1,W2) + tf.matmul(action_input,W2_action) + b2) 
q_value_output = tf.identity(tf.matmul(layer2,W3) + b3) 

其中self.variable被定义为:

def variable(self,shape,f): 
    return tf.Variable(tf.random_uniform(shape,-1/math.sqrt(f),1/math.sqrt(f))) 

忽略随机初始化(我只想结构),我试过如下:

state_in = cntk.input(state_dim, dtype=np.float32) 
action_in = cntk.input_variable(action_dim, dtype=np.float32) 

W1 = cntk.parameter(shape=(state_dim, layer1_size)) 
b1 = cntk.parameter(shape=(layer1_size)) 
W2 = cntk.parameter(shape=(layer1_size, layer2_size)) 
W2a = cntk.parameter(shape=(action_dim, layer2_size)) 
b2 = cntk.parameter(shape=(layer2_size)) 
W3 = cntk.parameter(shape=(layer2_size, 1)) 
b3 = cntk.parameter(shape=(1)) 

l1 = cntk.relu(cntk.times(state_in, W1) + b1) 
l2 = cntk.relu(cntk.times(l1, W2) + cntk.times(action_in, W2a) + b2) 
Q = cntk.times(l2, W3) + b3 

但是,二层的初始化失败,出现以下错误(片段):

RuntimeError: Operation 'Plus': Operand 'Output('Times24_Output_0', [#, *], [300])' has dynamic axes, that do not match the dynamic axes '[#]' of the other operands.

我想知道我做错了,如何准确地重新创建相同的模型。

回答

2

的原因是,你必须定义state_incntk.inputaction_incntk.input_variable在默认情况下略有不同的类型:cntk.input默认创建不能被绑定到的序列数据的变量,而cntk.input_variable默认创建必须是一个变量绑定到序列数据(NB input_variable已被弃用,并且一些像PyCharm这样的IDE会以删除线显示,请使用cntk.input()或cntk.sequence.input())。

错误说,加上操作不能添加cntk.times(l1, W2)具有动态轴[#]与cntk.times(action_in, W2a),其具有动态轴(意味着minibatch尺寸)[#,*](意味着minibatch和序列尺寸)。

最简单的解决方法是声明 action_in = cntk.input(action_dim, dtype=np.float32) 这使得其余的操作类型检查。

+0

谢谢!我的挫败感一定使我的视野蒙上阴影。 –