首页 > 解决方案 > tensorflow convolution result to numpy

问题描述

I write a simple code,

import numpy as np
import tensorflow as tf

x_data = np.loadtxt('D:\proj\dnn_lib_cuda\input')
w_data = np.loadtxt('D:\proj\dnn_lib_cuda\weight')

x_tensor = np.reshape(x_data, (1, 3, 224, 224))
w_tensor = np.reshape(w_data, (64, 3, 3, 3))

x_tensor_ch = x_tensor.transpose(0, 2, 3, 1)
w_tensor_ch = w_tensor.transpose(2, 3, 1, 0)


x = tf.placeholder(tf.float32, shape = (1, 224, 224, 3))
w = tf.placeholder(tf.float32, shape = (3, 3, 3, 64))

result = tf.nn.conv2d(input = x, filter = w, strides = [1, 1, 1, 1], padding = 'SAME')
sess = tf.Session()
sess.run(result, feed_dict = {x: x_tensor_ch, w:w_tensor_ch})
print(result)

Now the result is a tensor with shape (1, 224, 224, 64), how can I get the data with numpy format?

标签: pythonnumpytensorflow

解决方案


sess.run(...)result给定传递给的数据,返回计算张量的结果feed_dict

所以,你想要的是

output = sess.run(result, feed_dict = {x: x_tensor_ch, w:w_tensor_ch})

推荐阅读