答案 0 :(得分:0)
最简单的方法可能是编写自己的RNN单元。另一种方法是使用var id = 3;
var obj = {"comments":{"commentedBy":"test","date":"","comment":"Hello world","subComments":{"commentedBy":"jaril 2","date":"","comment":"Hello world inside dark","subComments":{"commentedBy":"jaril 3","date":"","comment":"wow working great","subComments":{"commentedBy":"jaril 4","date":"","comment":"wow working great","commentId":4},"commentId":3},"commentId":2},"commentId":1},"dueDate":"","createdDate":"","lastUpdated":"","checkList":[],"position":2,"status":"active"}
function deleteCommentId(comments, id) {
if (comments.subComments) {
if (comments.subComments.commentId === id) {
delete comments.subComments;
} else {
deleteCommentId(comments.subComments, id);
}
}
}
deleteCommentId(obj.comments, id);
console.log("final object==>",obj);
var expectedJSON = {"comments":{"commentedBy":"test","date":"","comment":"Hello world","subComments":{"commentedBy":"jaril 2","date":"","comment":"Hello world inside dark","commentId":2},"commentId":1},"dueDate":"","createdDate":"","lastUpdated":"","checkList":[],"position":2,"status":"active"}
console.log("Output match: ",JSON.stringify(obj) == JSON.stringify(expectedJSON));
。签出this post或此excellent article。
答案 1 :(得分:-1)
实际上,我按如下方式实现网络:
def build_model(self):
lstm_cell = tf.contrib.rnn.BasicLSTMCell(
num_units=self.config.num_lstm_units, state_is_tuple=True, reuse=True)
if self.mode == "train":
lstm_cell = tf.contrib.rnn.DropoutWrapper(
lstm_cell,
input_keep_prob=self.config.lstm_dropout_keep_prob,
output_keep_prob=self.config.lstm_dropout_keep_prob)
with tf.variable_scope("lstm", initializer=self.initializer) as lstm_scope:
zero_state = lstm_cell.zero_state(
batch_size=self.image_embeddings.get_shape()[0], dtype=tf.float32)
K = 5
C = 80
scores = tf.Variable(tf.random_normal(shape=[K, self.config.batch_size, C]), name="scores")
M = tf.Variable(tf.random_normal(shape=[K+1, self.config.batch_size, 2, 3]), name="M")
tf.assign(M[0], tf.convert_to_tensor([[1., 0., 0.], [0., 1., 0.]]))
lstm_input_size = 14
zk_size = 4096
hidden = zero_state
for k in range(0, K+1):
# Allow the LSTM variables to be reused.
if k > 0:
lstm_scope.reuse_variables()
f_k = spatial_transformer_network.spatial_transformer_network(self.image_embeddings, M[k])
f_k = tf.nn.max_pool(f_k, [1,2,2,1], [1,1,1,1], padding='VALID')
f_k = tf.layers.dense(tf.reshape(f_k, [self.config.batch_size, int(lstm_input_size * lstm_input_size / 4 * 512)]), 4096)
lstm_outputs, hidden = lstm_cell(f_k, hidden)
z_k = tf.layers.dense(hidden[0], zk_size, activation=tf.nn.relu)
if k != 0:
tf.assign(scores[k - 1], (tf.layers.dense(z_k, C)))
if k != K:
tf.assign(M[k + 1], (tf.reshape(tf.layers.dense(z_k, 6), [self.config.batch_size, 2, 3])))
tf.assign(M[k + 1, :, 0, 1], (tf.convert_to_tensor(0.)))
tf.assign(M[k + 1, :, 1, 0], (tf.convert_to_tensor(0.)))
但是运行时会抛出错误
lstm_outputs, hidden = lstm_cell(f_k, hidden).
错误信息是: ValueError:变量lstm / basic_lstm_cell / kernel不存在,或者不是使用tf.get_variable()创建的。您是要在VarScope中设置复用= tf.AUTO_REUSE吗?
那是什么问题?