我在将训练有序的顺序网络设置为Api类的类属性时遇到问题。
更准确地说,我正在运行一个本地python后端,以使用张量流进行回归预测。服务器在zerorpc上运行,代码如下
def parse_port():
return 4242
def main():
addr = 'tcp://127.0.0.1:' + str(parse_port())
network = createNetwork()
s = zerorpc.Server(Api(network))
s.bind(addr)
print('start running on {}'.format(addr))
s.run()
if __name__ == '__main__':
main()
class Api(object):
# Predict the value
network = None
def __init__(self, network):
self.network = network
def predict(self, param):
try:
return network.predict(param)
except Exception as e:
return 0.0
网络体系结构是在createNetwork函数中定义的。 (我必须手动创建架构,因为当前存在一个错误,您无法直接加载DenseFeature模型,而只能加载权重)
def createNetwork():
features = ['Feature1', 'Feature2', 'Feature3','Feature4', 'Feature5', 'Feature6', 'Feature7', 'Feature8', 'Feature9', 'Feature10', 'Feature11', 'Feature12', 'Feature13', "Feature14", "Output"]
predictionDataShape = np.transpose([[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1], [100]])
configData = pd.DataFrame(predictionDataShape, columns=features)
labels = configData.pop('Output')
ds=tf.data.Dataset.from_tensor_slices((dict(configData), labels))
ds = ds.batch(5)
# Create input feature layer
features.remove('Output')
feature_columns = []
for header in features:
feature_columns.append(feature_column_v2.numeric_column(header))
denseColumns = feature_column_v2.DenseFeatures(feature_columns)
model = tf.keras.Sequential([
denseColumns,
tf.keras.layers.Dense(256, activation="relu"),
tf.keras.layers.Dense(1, activation="relu"),
])
model.compile(optimizer=tf.keras.optimizers.RMSprop(0.001),
loss="mae",
metrics=['mean_absolute_error', 'mean_squared_error'])
model.fit(ds,
validation_data=ds,
epochs=1)
weightPath = os.path.join(os.getcwd(), 'src/python_backend/assets/weights.h5')
model.load_weights(weightPath)
return model
运行此命令时,会创建并初始化模型,但随后出现以下错误:
AttributeError: 'Sequential' object has no attribute '__name__'
似乎给Api变量赋值是问题,因为当我将该变量更改为其他变量时,一切都会运行。我究竟做错了什么?
谢谢:)