简体   繁体   English

从Keras / Tensor流量模型获得可用的系数权重

[英]Get Usable Co-efficient Weights from Keras/Tensor Flow Model

I've created a classification model using Keras (with tensorFlow backend). 我已经使用Keras(带有tensorFlow后端)创建了一个分类模型。 At this point, I'm looking to use model coefficients to create an web API service for my new model. 此时,我正在寻找使用模型系数为我的新模型创建Web API服务的方法。 However, when I attempt to get these weights, these are not as simple as multiply these coefficients * features to get expected results. 但是,当我尝试获得这些权重时,它们并不像将这些系数*特征相乘以获得预期结果那样简单。

My training input matrix is a grayscale image of 128 x 128 = 16,384. 我的训练输入矩阵是128 x 128 = 16,384的灰度图像。

 model.get_weights()
 [array([[[[-0.03603082,  0.1334779 ,  0.10018548, -0.14283592, -0.0429921 ,
       -0.03080634,  0.06451669, -0.09407537,  0.04878693, -0.14003059,
        0.04794825,  0.06447313, -0.01520954, -0.10879657,  0.13521752,
       -0.03230923, -0.01395164,  0.04935856,  0.06434418, -0.02601192,
        0.03416487,  0.08788931,  0.0723172 , -0.12923865,  0.04022292,
       -0.1328591 , -0.05803869, -0.01380468, -0.10409287,  0.14212781,
       -0.08511351, -0.03992498]],

     [[-0.11669005,  0.088085  , -0.......

  len(model.get_weights())
  #10

  len(model.get_weights()[0])
  #3

  len(model.get_weights()[0][0])
  #3

  len(model.get_weights()[0][0][0])
  #1

  len(model.get_weights()[0][0][0][0])
  #32

  len(model.get_weights()[0][0][0][0][0])
  #TypeError: object of type 'numpy.float32' has no len()

If I take all those level 10 * 3 * 3 * 1 * 32 = 2880. 如果我将所有这些级别都设为10 * 3 * 3 * 1 * 32 = 2880。

So this is telling me the numpy array has 5 dimensions with only 2880 values? 所以这告诉我numpy数组有5个维,只有2880个值? Maybe I'm missing something here or perhaps a much easier way. 也许我在这里错过了一些东西,或者是一种更简单的方法。 Any thoughts on how this could be applied to a new test image via an API? 关于如何通过API将其应用于新测试图像的任何想法?

The first layer has 288 (3*3*1*32) parameters, but following layers usually have much more parameters especially if your model is CNN. 第一层具有288(3 * 3 * 1 * 32)个参数,但是第二层通常具有更多的参数,尤其是在您的模型是CNN的情况下。

To print numbers of parameters for all layers (which include bias terms): 要打印所有层的参数数量(包括偏差项):

weights = model.get_weights()
for i in range(len(weights)):
    print(weights[i].shape)

Alternatively you can check the number of parameters by model.summary() 另外,您可以通过model.summary()检查参数数量

To apply the model to new images, just run model.predict(image) 要将模型应用于新图像,只需运行model.predict(image)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM