简体   繁体   English

为什么我不断收到名称错误:名称“模型”未定义

[英]Why do I keep getting Name Error: name 'model' is not defined

import numpy as np

from google.colab import files

from keras.preprocessing import image

from matplotlib import pyplot as plt

uploaded = files.upload()

for fn in uploaded.keys():

  path = fn

  img = image.load_img(path, target_size = (150, 150))

  imgplot = plt.imshow(img)

  x = image.img_to_array(img)

  x = np.expand_dims(x, axis = 0)

  images = np.vstack([x])

  classes = model.predict(images, batch_size=10)

  print(fn)

  print(classes)

  if classes[0][0]==1:

    print('Tangan ini menunjukkan BATU')

  elif classes[0][1]==1:

    print('Tangan ini menunjukkan GUNTING')

  elif classes[0][2]==1:

    print('Tangan ini menunjukkan KERTAS')

  else:

    print('TIDAK DIKETAHUI')

Please help请帮忙

Because model has not been defined before using it in the line classes = model.predict(images, batch_size=10) .因为model在行classes = model.predict(images, batch_size=10)中使用之前尚未定义。 First define a model and then use it, for eg;首先定义一个 model 然后使用它,例如;

from sklearn.linear_model import LinearRegression
    
x = 30 * np.random.random((20, 1))
y = 0.5 * x + 1.0 + np.random.normal(size=x.shape)
    
model = LinearRegression()
model.fit(x, y)
    
x_new = np.linspace(0, 30, 100)
y_new = model.predict(x_new[:, np.newaxis])

In this example, first a linear regression model is defined in the line model = LinearRegression() and then that model has been used to predict the new values in line y_new = model.predict(x_new[:, np.newaxis]) . In this example, first a linear regression model is defined in the line model = LinearRegression() and then that model has been used to predict the new values in line y_new = model.predict(x_new[:, np.newaxis]) .

Same thing you need to apply, first define a model that you want to use and then use it to predict the answer.你需要应用同样的东西,首先定义一个你想要使用的 model 然后用它来预测答案。 Otherwise if you are using a predefined model from somewhere, you need to import it into your program.否则,如果您从某个地方使用预定义的 model,则需要将其导入程序中。

Model is not defined. Model 未定义。

You need to instantiate before using it.您需要在使用它之前进行实例化。

Looks like you are using keras.看起来您正在使用 keras。

Here is the documentation for their model API.这是他们的 model API 的文档。

https://keras.io/api/models/model/#model-class https://keras.io/api/models/model/#model-class

Below is an example of instantiating a model from their API docs.下面是从他们的 API 文档中实例化 model 的示例。

import tensorflow as tf

inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)

in your code you have在你的代码中

classes = model.predict(images, batch_size=10)

have you imported model?你有进口model吗? (or make sure if it exists in you libraries imported) (或确保它是否存在于您导入的库中)

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

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