简体   繁体   中英

what is the equivalent keras function for from_numpy in torch?

I found a code in torch and I have to change it to keras, but I could not find some equivalent for some of them. for example, I changed some of them as follow, but I am not sure they are true or not:

 `torch.tensor` to `K.variable` ( `K` is `from keras import backend as K`)
  unsqueez_(1) to K.expand_dims
  torch.empty((3,) + requested_shape) to K.zeros((3,) + requested_shape)

but I could not find anything for torch.from_numpy . now, my question is about the above changes I did that are they true? and something similar to torch.from_numpy ? I appreciate your help.

You can just initialize a variable with numpy array like this:

ary = np.random.normal(size=(2, 2))
v = K.variable(ary)

or use cast() to convert numpy array to a tensor:

ary = np.random.normal(size=(2, 2))
tensor = K.cast(ary, dtype='float32')

Beside this, the code that you used is correct.

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