keras 无法使用 reshape 重塑 keras 张量
keras can not reshape keras tesnor using reshape
我正在尝试使用重塑层重塑张量:
from keras.layers.convolutional import Conv2D, MaxPooling2D,AveragePooling2D
from keras import backend as K
from keras.models import Model
from keras.layers import Input
from keras.layers.core import Activation, Reshape
from keras.layers import Dense,Reshape,Lambda,Dropout
import numpy as np
from keras.layers.embeddings import Embedding
Dict_size=32
EmbedSz=16
img_sz=100
channels=3
input=Input(shape=(img_sz,img_sz,channels))
H=Conv2D(Dict_size, 3, 3, activation='relu', border_mode='same')(input)
H=Lambda(lambda x:K.argmax(x, axis=3),output_shape=lambda s: (img_sz,img_sz,))(H)
H=Reshape((1,img_sz*img_sz))(H)
model=Model(inputs=input,outputs=H)
#model.compile( optimizer= 'adam', metrics=[ 'accuracy' ],loss='mse')
ar=np.random.rand(1,100,100,3)
pr=model.predict(ar)
print(pr.shape)
print(pr)$
但是出现这个错误!
文件“/usr/local/lib/python2.7/dist-packages/keras/layers/core.py”,第 379 行,在 _fix_unknown_dimension 中
提高 ValueError(msg)
ValueError:新数组的总大小必须不变
我没有改尺寸!!
您只是忘记将批量大小维度添加到您的 Lambda 层:
H= Lambda(lambda x: K.argmax(x, axis=3), output_shape=lambda s: (None, img_sz,img_sz,))(H)
# ^
# |
因此,只需将 None
添加到 output_shape。
我正在尝试使用重塑层重塑张量:
from keras.layers.convolutional import Conv2D, MaxPooling2D,AveragePooling2D
from keras import backend as K
from keras.models import Model
from keras.layers import Input
from keras.layers.core import Activation, Reshape
from keras.layers import Dense,Reshape,Lambda,Dropout
import numpy as np
from keras.layers.embeddings import Embedding
Dict_size=32
EmbedSz=16
img_sz=100
channels=3
input=Input(shape=(img_sz,img_sz,channels))
H=Conv2D(Dict_size, 3, 3, activation='relu', border_mode='same')(input)
H=Lambda(lambda x:K.argmax(x, axis=3),output_shape=lambda s: (img_sz,img_sz,))(H)
H=Reshape((1,img_sz*img_sz))(H)
model=Model(inputs=input,outputs=H)
#model.compile( optimizer= 'adam', metrics=[ 'accuracy' ],loss='mse')
ar=np.random.rand(1,100,100,3)
pr=model.predict(ar)
print(pr.shape)
print(pr)$
但是出现这个错误! 文件“/usr/local/lib/python2.7/dist-packages/keras/layers/core.py”,第 379 行,在 _fix_unknown_dimension 中 提高 ValueError(msg) ValueError:新数组的总大小必须不变
我没有改尺寸!!
您只是忘记将批量大小维度添加到您的 Lambda 层:
H= Lambda(lambda x: K.argmax(x, axis=3), output_shape=lambda s: (None, img_sz,img_sz,))(H)
# ^
# |
因此,只需将 None
添加到 output_shape。