python-3.x - 模块“tensorflow”没有属性“random_uniform”
问题描述
我尝试执行一些深度学习应用程序并得到一个模块“tensorflow”没有属性“random_uniform”错误。在 CPU 上,代码运行良好,但速度非常慢。为了在 GPU 上运行代码,我需要更改一些定义。下面是我的代码。有任何想法吗?
def CapsNet(input_shape, n_class, routings):
x = tf.keras.layers.Input(shape=input_shape)
# Layer 1: Just a conventional Conv2D layer
conv1 = tf.keras.layers.Convolution2D(filters=256, kernel_size=9, strides=1, padding='valid', activation='relu', name='conv1')(x)
# Layer 2: Conv2D layer with `squash` activation, then reshape to [None, num_capsule, dim_capsule]
primarycaps = PrimaryCap(conv1, dim_capsule=8, n_channels=32, kernel_size=9, strides=2, padding='valid')
# Layer 3: Capsule layer. Routing algorithm works here.
digitcaps = CapsuleLayer(num_capsule=n_class, dim_capsule=16, routings=routings,
name='digitcaps')(primarycaps)
# Layer 4: This is an auxiliary layer to replace each capsule with its length. Just to match the true label's shape.
# If using tensorflow, this will not be necessary. :)
out_caps = Length(name='capsnet')(digitcaps)
# Decoder network.
y = tf.keras.layers.Input(shape=(n_class,))
masked_by_y = Mask()([digitcaps, y]) # The true label is used to mask the output of capsule layer. For training
masked = Mask()(digitcaps) # Mask using the capsule with maximal length. For prediction
# Shared Decoder model in training and prediction
decoder = tf.keras.models.Sequential(name='decoder')
decoder.add(tf.keras.layers.Dense(512, activation='relu', input_dim=16*n_class))
decoder.add(tf.keras.layers.Dense(1024, activation='relu'))
decoder.add(tf.keras.layers.Dense(np.prod(input_shape), activation='sigmoid'))
decoder.add(tf.keras.layers.Reshape(target_shape=input_shape, name='out_recon'))
# Models for training and evaluation (prediction)
train_model = tf.keras.models.Model([x, y], [out_caps, decoder(masked_by_y)])
eval_model = tf.keras.models.Model(x, [out_caps, decoder(masked)])
# manipulate model
noise = tf.keras.layers.Input(shape=(n_class, 16))
noised_digitcaps = tf.keras.layers.Add()([digitcaps, noise])
masked_noised_y = Mask()([noised_digitcaps, y])
manipulate_model = tf.keras.models.Model([x, y, noise], decoder(masked_noised_y))
return train_model, eval_model, manipulate_model
def margin_loss(y_true, y_pred):
L = y_true * K.square(K.maximum(0., 0.9 - y_pred)) + \
0.5 * (1 - y_true) * K.square(K.maximum(0., y_pred - 0.1))
return K.mean(K.sum(L, 1))
model, eval_model, manipulate_model = CapsNet(input_shape=train_x_temp.shape[1:], n_class=len(np.unique(np.argmax(train_y, 1))), routings=3)
解决方案
问题在于您的tenserflow安装。确切地说,您的 python tensorflow 库。确保您正确地重新安装了软件包,使用 anaconda 您需要以管理员权限安装它。
或者你有最新版本,那么你需要添加喜欢
tf.random.uniform(
有关更多信息,请参阅文档:https ://www.tensorflow.org/api_docs/python/tf/random/uniform
推荐阅读
- amazon-web-services - AWS AppFlow 数据从 Salesforce 加载到 S3 初始化架构错误:SystemModstamp 中的非法字符
- angular - 角度 5 中的 Package-lock.json 文件造成问题
- javascript - 基本“+”运算符的舍入错误
- c++ - 在函数中间更改变量名
- django - Django querySet 按最接近今天的日期排序
- terraform - 有没有办法从 terraform 中的默认标签中排除资源
- typescript - 为什么 TypeScript 中的 & 运算符过滤联合类型但连接对象类型?
- python-3.x - python,拆分,正则表达式和组合重新
- python - Heroku 部署深度学习模型
- html - 如何将电子表格变成图像?