【问题标题】:Save ft.tensor array with np.savetxt - Python/TensorFlow使用 np.savetxt 保存 ft.tensor 数组 - Python/TensorFlow
【发布时间】:2018-03-13 20:44:55
【问题描述】:

我创建此函数是为了将变量 layer 的所有值保存在外部文件中:

# Counter for total number of iterations performed so far
total_iterations = 0

def test_save(num_iterations):
    # Ensure we update the global variable rather than a local copy
    global total_iterations

    for i in range(total_iterations, total_iterations + num_iterations):

        x_batch, y_true_batch = next_batch_size(train_batch_size)

        feed_dict_train = {x: x_batch, y_true: y_true_batch}

        # Message for printing
        msg = " Iteration: {0:>6}"

        # Print it
        print(msg.format(i + 1))

        test = session.run(layer, feed_dict=feed_dict_train)

        print 'test',test

        store_all = []

        store_all.append(test)

    np.savetxt('test.txt', store_all, fmt='%5s')

# Call function
test_save(300)

但我的附加似乎不起作用,因为当我打开我的 test.txt 文件时,只有 1 层而不是 300 个结果。

我的占位符是:

# Placeholder variable for the input images
x = tf.placeholder(tf.float32, shape=[None, img_size_flat], name='x')

# Reshape 'x'
x_image = tf.reshape(x, [-1, img_size, img_size, num_channels])

# Placeholder variable for the true labels associated with the images
y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')

我的层是:

<tf.Tensor 'Conv2D_1:0' shape=(?, 16, 16, 1) dtype=float32>

【问题讨论】:

    标签: python python-2.7 numpy tensorflow tensor


    【解决方案1】:

    您在循环的每次迭代中都初始化了一个空列表。把它放在外面:

    # Counter for total number of iterations performed so far
    total_iterations = 0
    
    def test_save(num_iterations):
        # Ensure we update the global variable rather than a local copy
        global total_iterations
    
        store_all = []
    
        for i in range(total_iterations, total_iterations + num_iterations):
    
            x_batch, y_true_batch = next_batch_size(train_batch_size)
    
            feed_dict_train = {x: x_batch, y_true: y_true_batch}
    
            # Message for printing
            msg = " Iteration: {0:>6}"
    
            # Print it
            print(msg.format(i + 1))
    
            test = session.run(layer, feed_dict=feed_dict_train)
    
            print 'test',test
    
            store_all.append(test)
    
        np.savetxt('test.txt', store_all, fmt='%5s')
    
    # Call function
    test_save(300)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-06
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 2015-09-29
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多