简体   繁体   English

如何将此张量转换为 numpy 数组?

[英]How can I convert this tensor to a numpy array?

I'd like to apply the pagerank algorithm to the x_attn tensor.我想将 pagerank 算法应用于 x_attn 张量。 But the nx.pagerank module only accepts numpy arrays.但是 nx.pagerank 模块只接受 numpy 数组。 When I try to convert it to using x_att.eval() , it says:当我尝试将其转换为使用x_att.eval() 时,它说:

"tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'main_input_5' with dtype float and shape [?,6600]". “tensorflow.python.framework.errors_impl.InvalidArgumentError:您必须为占位符张量‘main_input_5’提供一个值,其数据类型为浮点型和形状 [?,6600]”。

Can somebody please help me out?有人可以帮我吗?

def variable_attn_15jan():


    input_dim=input_dim_func()

    main_input = Input(shape=(input_dim,),name='main_input')

    inputs_w1=Lambda(lambda x: x[:,0:3300])(main_input)  
    inputs_w2=Lambda(lambda x: x[:,3300:6600])(main_input) 


    x1_attn= Dense(11, activation='softmax')(inputs_w1)
    x2_attn= Dense(11, activation='softmax')(inputs_w2)


    list_x_att1=[]
    list_x_att2=[]

    for i in range(11) :
        val_scalar=Lambda(lambda x: x[:,i:(i+1)])(x1_attn)
        list_x_att1.append(Lambda(lambda x: x[:,(i*300):(i+1)*300]*val_scalar)(inputs_w1))
    x_att1 = concatenate(list_x_att1)

    for i in range(11) :
        val_scalar=Lambda(lambda x: x[:,i:(i+1)])(x2_attn)
        list_x_att2.append(Lambda(lambda x: x[:,(i*300):(i+1)*300]*val_scalar)(inputs_w2))
    x_att2 = concatenate(list_x_att2)

    x_att = concatenate([x_att1,x_att2])


On your tensor (v2.0):在你的张量 (v2.0) 上:

npa = tf.numpy()

where npa will be your numpy array name.其中 npa 将是您的 numpy 数组名称。

Alternatively, tensor (< v2.0):或者,张量 (< v2.0):

npa=tf.eval()
print(type(npa))

Update 1:更新 1:

Use below code how to check what type of arrays you've got.使用以下代码如何检查您拥有的数组类型。 Look also at comment from rkern commented posted here on Jul 14, 2019 .另请查看rkern于 2019 年 7 月 14 日在此处发表的评论。

type(tf)

type(np.array(tf))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM