馆长King 2022-11-01 00:14 采纳率: 0%
浏览 40
已结题

将模型中的Tensor转为numpy

问题遇到的现象和发生背景

将tensor转换为numpy

用代码块功能插入代码,请勿粘贴截图
from keras import backend as K

in_shp = train_data.shape[1:]  # in_shap= (40962)
Xm_input = Input(in_shp)  
x1 = Lambda(slice,output_shape=(4096,1),arguments={'index':0})(Xm_input)
x2 = Lambda(slice,output_shape=(4096,1),arguments={'index':1})(Xm_input)
n1 = K.eval(x1)
model.compile(optimizer = Nadam(learning_rate=0.002),
loss= 'categorical_crossentropy',
metrics=[iou_coef],
run_eagerly=True)
运行结果及报错内容

('Some keys in session_kwargs are not supported at this time: %s', dict_keys(['run_eagerly']))

我的解答思路和尝试过的方法

想要将lambda层输出的Tensor转为numpy,刚开始会报placehold的错误,之后再compile中加了run_eagerly参数,又报了新的错误:('Some keys in session_kwargs are not supported at this time: %s', dict_keys(['run_eagerly']))

我想要达到的结果

将模型中的Tensor转为numpy

  • 写回答

6条回答 默认 最新

  • prince_zxill 2022-11-08 08:00
    关注

    NumPy 数组转 Tensor:

    import numpy as np
    a = np.ones(7)
    b = torch.from_numpy(a)
    print(a, b)
    
    a += 1
    print(a, b)
    b += 1
    print(a, b)
    
    
    
    [1. 1. 1. 1. 1. 1. 1.] tensor([1., 1., 1., 1., 1., 1., 1.], dtype=torch.float64)
    [2. 2. 2. 2. 2. 2. 2.] tensor([2., 2., 2., 2., 2., 2., 2.], dtype=torch.float64)
    [3. 3. 3. 3. 3. 3. 3.] tensor([3., 3., 3., 3., 3., 3., 3.], dtype=torch.float64)
    
    
    
    评论

报告相同问题?

问题事件

  • 系统已结题 11月9日
  • 创建了问题 11月1日

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作