keras 张量切片
对张量切片的方式和numpy 一样,如下
out1=outs[:,:100]
out2=outs[:,100:]
下面是代码demo
import keras.backend as K
from tensorflow.keras.layers import concatenate
from tensorflow.keras import Sequential,Model
from tensorflow.keras.layers import Dense ,Concatenate,Input,BatchNormalization
dense_inputs1 = Input(shape=(1024, ))
dense_inputs2 = Input(shape=(512, ))
d1 = Dense(256, activation='relu')(dense_inputs1)
d2 = Dense(256, activation='relu')(dense_inputs2)
merge_inputs = concatenate([d1,d2],axis=1)
flow_dense = Dense(256, activation='relu')(merge_inputs)
flow_dense = BatchNormalization()( flow_dense)
outs = Dense(100+1, activation='softmax')(flow_dense)
print(outs)
原来的张量这是101维度的张量
Tensor("dense_11/Softmax:0", shape=(?, 101), dtype=float32)
101维度的张量已经切割成100维和1维
out1=outs[:,:100]
out2=outs[:,100:]
print(out1)
print(out2)
Tensor("strided_slice_10:0", shape=(?, 100), dtype=float32)
Tensor("strided_slice_11:0", shape=(?, 1), dtype=float32)