CNTK 中以下 tensorflow 片段的等价物是什么

What is the equivalent of the following tensorflow snippet in CNTK

我正在尝试在 CNTK 中实现 DDPG 并遇到了以下代码(使用 Tensorflow)来创建评论家网络:

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

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

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

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

原因是您将 state_in 定义为 cntk.input,将 action_in 定义为 cntk.input_variable,默认情况下它们的类型略有不同:默认情况下 cntk.input创建一个不能绑定到序列数据的变量,而 cntk.input_variable 默认创建一个必须绑定到序列数据的变量(N.B。input_variable 已被弃用,一些 IDE 如 PyCharm 将用删除线显示,请使用 cntk.input() 或 cntk.sequence.input())。

错误说加法运算不能将具有动态轴[#](意味着小批量维度)的cntk.times(l1, W2)与具有动态轴[#, *](意味着小批量和序列维度)。

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