模块'tensorflow'没有属性'random_uniform' [英] module 'tensorflow' has no attribute 'random_uniform'

查看:228
本文介绍了模块'tensorflow'没有属性'random_uniform'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试执行一些深度学习应用程序,并得到了一个模块'tensorflow'没有属性'random_uniform'错误.在CPU上,代码可以正常工作,但速度确实很慢.为了在GPU上运行代码,我需要更改一些定义.这是我下面的代码.有什么想法吗?

I tried to perform some deep learning application and got a module 'tensorflow' has no attribute 'random_uniform' error. On CPU the code works fine but it is really slow. In order to run the code on GPU i needed to change some definitions. Here is my code below. Any ideas?

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才能以管理员权限进行安装.

The problem lays with your tenserflow installation. To be exact your python tensorflow library. Make sure you reinstall the package correctly, with anaconda you need to install it with administrator rights.

或者您拥有最新版本,则需要添加类似内容

Or you have the newest version then you need to add like

tf.random.uniform(

有关文档的更多信息,请参见: https://www.tensorflow.org/api_docs/python/tf/random/uniform

See for more information the documentation: https://www.tensorflow.org/api_docs/python/tf/random/uniform

这篇关于模块'tensorflow'没有属性'random_uniform'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆