为什么Tensorboard图表中的所有内容都已断开连接?

时间:2018-05-20 13:29:46

标签: python tensorflow machine-learning deep-learning tensorboard

我已经实施了一个使用accelrometer数据检测人类活动的CNN,我的模型工作得很好但是当我在张量板上显示我的grapgh时,所有东西似乎都是双向的。现在我没有使用Namescopes,但即使没有它,grpagh也应该有道理吗?

Tensorboard Graph

编辑在实现@ user1735003给出的答案后,这是输出。我仍然不明白为什么我得到了左边的所有节点

enter image description here

我实现的是:我有两个卷积层和两个最大池层,最重要的是我有两个隐藏层,其中 1024 512 神经元。

所以这是我的代码:

#Weights
def init_weights(shape):
    init_random_dist = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(init_random_dist)


#Bias
def init_bias(shape):
    init_bias = tf.constant(0.1,shape=shape)
    return tf.Variable(init_bias)

def conv1d(x,weights):
    #x is input accelration data and W is corresponding weight
    return tf.nn.conv1d(value=x,filters = weights,stride=1,padding='VALID')

def convolution_layer(input_x,shape):
   w1 = init_weights(shape)
   b = init_bias([shape[2]])
   return tf.nn.relu(conv1d(input_x,weights=w1)+b)


def normal_full_layer(input_layer,size):
    input_size = int(input_layer.get_shape()[1])
    W = init_weights([input_size, size])
    b = init_bias([size])
    return tf.matmul(input_layer, W) +b


x = tf.placeholder(tf.float32,shape=[None ,window_size,3]) #input tensor with 3 input channels
y = tf.placeholder(tf.float32,shape=[None,6]) #Labels

con_layer_1 = convolution_layer(x,shape=[4,3,32])#filter  of shape [filter_width, in_channels, out_channels]

max_pool_1=tf.layers.max_pooling1d(inputs=con_layer_1,pool_size=2,strides=2,padding='Valid')

con_layer_2 = convolution_layer(max_pool_1,shape=[4,32,64])

max_pool_2 = tf.layers.max_pooling1d(inputs=con_layer_2,pool_size=2,strides=2,padding='Valid')

flat = tf.reshape(max_pool_2,[-1,max_pool_2.get_shape()[1]*max_pool_2.get_shape()[2]])

fully_conected = tf.nn.relu(normal_full_layer(flat,1024))


second_hidden_layer = tf.nn.relu(normal_full_layer(fully_conected,512))
hold_prob = tf.placeholder(tf.float32)
full_one_dropout = tf.nn.dropout(second_hidden_layer,keep_prob=hold_prob)


y_pred = normal_full_layer(full_one_dropout,6)
pred_softmax = tf.nn.softmax(y_pred)


cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=y_pred))

optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
train = optimizer.minimize(cross_entropy)
init = tf.global_variables_initializer()




with tf.Session() as sess:
sess.run(init)
filename="./summary_log11/run"
summary_writer = tf.summary.FileWriter(filename, graph_def=sess.graph_def)

for i in range(5000):
    batch_x,batch_y = next_batch(100,X_train,y_train)
    sess.run(train, feed_dict={x: batch_x, y: batch_y, hold_prob: 0.5})

    # PRINT OUT A MESSAGE EVERY 100 STEPS
    if i%100 == 0:

        print('Currently on step {}'.format(i))
        print('Accuracy is:')
        # Test the Train Model
        matches = tf.equal(tf.argmax(y_pred,1),tf.argmax(y,1))

        acc = tf.reduce_mean(tf.cast(matches,tf.float32))

        print(sess.run(acc,feed_dict={x:X_test,y:y_test,hold_prob:1.0}))
        print('\n')

2 个答案:

答案 0 :(得分:2)

尝试将节点组织到范围中。这将有助于Tensorboard找出您的图层次结构。例如,

with tf.variable_scope('input'):
    x = tf.placeholder(tf.float32,shape=[None ,window_size,3]) #input tensor with 3 input channels
    y = tf.placeholder(tf.float32,shape=[None,6]) #Labels

with tf.variable_scope('net'):

    con_layer_1 = convolution_layer(x,shape=[4,3,32])#filter  of shape [filter_width, in_channels, out_channels]

    max_pool_1=tf.layers.max_pooling1d(inputs=con_layer_1,pool_size=2,strides=2,padding='Valid')

    con_layer_2 = convolution_layer(max_pool_1,shape=[4,32,64])

    max_pool_2 = tf.layers.max_pooling1d(inputs=con_layer_2,pool_size=2,strides=2,padding='Valid')

    flat = tf.reshape(max_pool_2,[-1,max_pool_2.get_shape()[1]*max_pool_2.get_shape()[2]])

    fully_conected = tf.nn.relu(normal_full_layer(flat,1024))


    second_hidden_layer = tf.nn.relu(normal_full_layer(fully_conected,512))
    hold_prob = tf.placeholder(tf.float32)
    full_one_dropout = tf.nn.dropout(second_hidden_layer,keep_prob=hold_prob)


    y_pred = normal_full_layer(full_one_dropout,6)
    pred_softmax = tf.nn.softmax(y_pred)

with tf.variable_scope('loss'):

    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=y_pred))

with tf.variable_scope('optimizer'):
    optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
    train = optimizer.minimize(cross_entropy)

答案 1 :(得分:1)

由于您没有明确指定您的tf操作,因此它由tensorflow自动完成,例如: ReLu运算符名为ReLu_1ReLu_2,....根据{{​​3}}:

  

最后一个结构简化是系列折叠。顺序主题 - 即节点,其名称在末尾以数字相互作用并具有同构结构 - 被折叠成单个节点堆栈,如下所示。对于具有长序列的网络,这极大地简化了视图。

正如您在图表的右侧所看到的,所有add_[0-7]MatMul_[0-5]Relu_[0-5]节点都归为一类,因为它们的名称相似,这并不意味着节点在图表中断开连接,它只是张量板的节点分组策略。

如果您想避免这种情况,那么请为您的操作提供与最后一个数字不同的名称。或者如前所述使用tensorboard documentation,例如:

with tf.name_scope("conv1"):
  con_layer_1 = convolution_layer(x,shape=[4,3,32])
  max_pool_1=tf.layers.max_pooling1d(inputs=con_layer_1,pool_size=2,strides=2,padding='Valid')

with tf.name_scope("conv2"):
  con_layer_2 = convolution_layer(max_pool_1,shape=[4,32,64])
  max_pool_2 = tf.layers.max_pooling1d(inputs=con_layer_2,pool_size=2,strides=2,padding='Valid')

# etc.