【问题标题】:TypeError: 'int' object is not callable in np.random.seedTypeError:“int”对象在 np.random.seed 中不可调用
【发布时间】:2020-12-23 02:04:45
【问题描述】:

我正在尝试在 2018 年 Data Science Bowl 之前的 Kaggle 比赛中进行数据增强。我正在尝试这段代码:

## Data augmentation
# Creating the training Image and Mask generator
image_datagen = image.ImageDataGenerator(shear_range=0.5, rotation_range=50, zoom_range=0.2, width_shift_range=0.2, height_shift_range=0.2, fill_mode='reflect')
mask_datagen = image.ImageDataGenerator(shear_range=0.5, rotation_range=50, zoom_range=0.2, width_shift_range=0.2, height_shift_range=0.2, fill_mode='reflect')

# Keep the same seed for image and mask generators so they fit together
image_datagen.fit(X_train[:int(X_train.shape[0]*0.9)], augment=True, seed=42)
mask_datagen.fit(Y_train[:int(Y_train.shape[0]*0.9)], augment=True, seed=42)

x=image_datagen.flow(X_train[:int(X_train.shape[0]*0.9)],batch_size=BATCH_SIZE,shuffle=True, seed=42)
y=mask_datagen.flow(Y_train[:int(Y_train.shape[0]*0.9)],batch_size=BATCH_SIZE,shuffle=True, seed=seed)



# Creating the validation Image and Mask generator
image_datagen_val = image.ImageDataGenerator()
mask_datagen_val = image.ImageDataGenerator()

image_datagen_val.fit(X_train[int(X_train.shape[0]*0.9):], augment=True, seed=seed)
mask_datagen_val.fit(Y_train[int(Y_train.shape[0]*0.9):], augment=True, seed=seed)

x_val=image_datagen_val.flow(X_train[int(X_train.shape[0]*0.9):],batch_size=BATCH_SIZE,shuffle=True, seed=seed)
y_val=mask_datagen_val.flow(Y_train[int(Y_train.shape[0]*0.9):],batch_size=BATCH_SIZE,shuffle=True, seed=seed)

这是错误信息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-126-6b608552652e> in <module>
      5 
      6 # Keep the same seed for image and mask generators so they fit together
----> 7 image_datagen.fit(X_train[:int(X_train.shape[0]*0.9)], augment=True, seed=42)
      8 mask_datagen.fit(Y_train[:int(Y_train.shape[0]*0.9)], augment=True, seed=42)
      9 

~\Anaconda3\lib\site-packages\keras_preprocessing\image\image_data_generator.py in fit(self, x, augment, rounds, seed)
    941 
    942         if seed is not None:
--> 943             np.random.seed(seed)
    944 
    945         x = np.copy(x)

TypeError: 'int' object is not callable

据我了解,错误出现在image_datagen.fitseed 参数中。就我而言,错误消息在fit 代码中显示了一些内部问题。我不明白为什么。

我探索了其他类似的问题,但我发现它们都不适合我的问题。

这些是我读过的解决方案:

Getting TypeError: 'int' object is not callable

Python "int object is not callable"

class method TypeError "Int object not callable"

【问题讨论】:

  • 显然您在此之前的某个时间点为np.random.seed 分配了一些东西。不要那样做。这不是你使用np.random.seed的方式。
  • 我之前在计算切片值以创建切片 start:end 之前遇到过问题。如果您在调用之前使用将切片的停止点分配给变量,您会得到相同的错误吗?
  • @mgrollins ,我仍然面临同样的错误。 user2357112 是对的。谢谢你们俩
  • 在您的代码中的某个时刻,您执行了np.random.seed = 10(10 只是一个示例,可以是任何其他数字)。删除该行,重新启动内核并立即使用np.random.seed(10) 重试
  • 感谢@Noussa 的更新!祝你好运!

标签: python numpy random


【解决方案1】:

确保您np.random.seed 分配给脚本中某处的某个整数

像这样:

np.random.seed = 42

【讨论】:

  • 太棒了!您一定是这样做的,并且在很长一段时间后自己想通了!
【解决方案2】:

你已经像这样初始化了种子值:

np.random.seed = 42

试试这个:

np.random.seed(42)

然后再次运行完整代码

【讨论】:

    猜你喜欢
    • 2012-04-03
    • 1970-01-01
    • 2020-01-10
    • 2013-04-03
    • 2017-12-20
    • 2023-03-03
    相关资源
    最近更新 更多