tensorflow中tensor列(行)选取

tensorflow中tensor列(行)选取

今天学习Brain-tumor-segmentation-master代码,在尝试执行的过程中出现报错:
TypeError: Only integers, slices (:), ellipsis (...), tf.newaxis (None) and scalar tf.int32/tf.int64 tensors are valid indices, got [1,3]
定位报错位置:
tensorflow中tensor列(行)选取_第1张图片

检查发现报错原因是张量y_true_f选取特定列的表达式有误,百度解决方案基本都是shit,汇总信息后得到的解决方案如下:

y_core=K.sum(y_true_f[:,[1,3]],axis=1)

改为

tmp=tf.gather(y_true_f,[1,3],axis=1)
y_core=K.sum(tmp,axis=1)

附录:tensorflow下tensorl列(行)操作
1、选取某一列(行)或者第x列(行)到第y列(行),可以像操作数组一样书写表达式
参考链接:https://blog.csdn.net/weixin_44613063/article/details/103571787
2、选取某几列(行)需利用gather函数
参考链接:https://blog.csdn.net/qq_25964837/article/details/80462908

你可能感兴趣的:(tensorflow,人工智能,python)