简体   繁体   中英

How can I convert this tensor to a numpy array?

I'd like to apply the pagerank algorithm to the x_attn tensor. But the nx.pagerank module only accepts numpy arrays. When I try to convert it to using x_att.eval() , it says:

"tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'main_input_5' with dtype float and shape [?,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):

npa = tf.numpy()

where npa will be your numpy array name.

Alternatively, tensor (< v2.0):

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

Update 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 .

type(tf)

type(np.array(tf))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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