张量流卷积结果到numpy

2024-04-18 06:39:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个简单的代码

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)

现在的结果是一个形状张量(1,224,224,64),我怎样才能得到numpy格式的数据


Tags: importnumpyinputdatalibtfasnp
1条回答
网友
1楼 · 发布于 2024-04-18 06:39:18

sess.run(...)返回给定传递给feed_dict的数据对张量result求值的结果

所以,你想要的是

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

相关问题 更多 >