首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >模块“tensorflow.compat.v1”没有属性“cont肋骨”

模块“tensorflow.compat.v1”没有属性“cont肋骨”
EN

Stack Overflow用户
提问于 2021-04-24 15:04:23
回答 2查看 7.6K关注 0票数 2

我必须导入tensorflow,如下所示,因为tensorflow 2不支持“占位符”函数:

代码语言:javascript
运行
复制
import tensorflow.compat.v1 as tf

tf.disable_v2_behavior() 

此外,代码片段在下面的co中使用了'contrib‘函数。

代码语言:javascript
运行
复制
def encoding_layer(rnn_size, sequence_length, num_layers, rnn_inputs, keep_prob):
    '''Create the encoding layer'''
    
    for layer in range(num_layers):
        with tf.variable_scope('encoder_{}'.format(layer)):
            cell_fw = tf.contrib.rnn.LSTMCell(rnn_size,
                                              initializer=tf.random_uniform_initializer(-0.1, 0.1, seed=2))
            cell_fw = tf.contrib.rnn.DropoutWrapper(cell_fw, 
                                                    input_keep_prob = keep_prob)

            cell_bw = tf.contrib.rnn.LSTMCell(rnn_size,
                                              initializer=tf.random_uniform_initializer(-0.1, 0.1, seed=2))
            cell_bw = tf.contrib.rnn.DropoutWrapper(cell_bw, 
                                                    input_keep_prob = keep_prob)

            enc_output, enc_state = tf.nn.bidirectional_dynamic_rnn(cell_fw, 
                                                                    cell_bw, 
                                                                    rnn_inputs,
                                                                    sequence_length,
                                                                    dtype=tf.float32)
    # Join outputs since we are using a bidirectional RNN
    enc_output = tf.concat(enc_output,2)
    
    return enc_output, enc_state

最后的代码块是:

代码语言:javascript
运行
复制
# Build the graph
train_graph = tf.Graph()
# Set the graph to default to ensure that it is ready for training
with train_graph.as_default():
    
    # Load the model inputs    
    input_data, targets, lr, keep_prob, summary_length, max_summary_length, text_length = model_inputs()

    # Create the training and inference logits
    training_logits, inference_logits = seq2seq_model(tf.reverse(input_data, [-1]),
                                                      targets, 
                                                      keep_prob,   
                                                      text_length,
                                                      summary_length,
                                                      max_summary_length,
                                                      len(vocab_to_int)+1,
                                                      rnn_size, 
                                                      num_layers, 
                                                      vocab_to_int,
                                                      batch_size)
    
    # Create tensors for the training logits and inference logits
    training_logits = tf.identity(training_logits.rnn_output, 'logits')
    inference_logits = tf.identity(inference_logits.sample_id, name='predictions')
    
    # Create the weights for sequence_loss
    masks = tf.sequence_mask(summary_length, max_summary_length, dtype=tf.float32, name='masks')

    with tf.name_scope("optimization"):
        # Loss function
        cost = tf.contrib.seq2seq.sequence_loss(
            training_logits,
            targets,
            masks)

        # Optimizer
        optimizer = tf.train.AdamOptimizer(learning_rate)

        # Gradient Clipping
        gradients = optimizer.compute_gradients(cost)
        capped_gradients = [(tf.clip_by_value(grad, -5., 5.), var) for grad, var in gradients if grad is not None]
        train_op = optimizer.apply_gradients(capped_gradients)
print("Graph is built.")

我得到以下错误:

代码语言:javascript
运行
复制
AttributeError: module 'tensorflow.compat.v1' has no attribute 'contrib'

但是,我遇到了一些建议我安装tensorflow 1.14的答案,这也会导致以下错误:

代码语言:javascript
运行
复制
❯ pip install tensorflow==1.14
ERROR: Could not find a version that satisfies the requirement tensorflow==1.14 (from versions: 2.2.0rc1, 2.2.0rc2, 2.2.0rc3, 2.2.0rc4, 2.2.0, 2.2.1, 2.2.2, 2.3.0rc0, 2.3.0rc1, 2.3.0rc2, 2.3.0, 2.3.1, 2.3.2, 2.4.0rc0, 2.4.0rc1, 2.4.0rc2, 2.4.0rc3, 2.4.0rc4, 2.4.0, 2.4.1, 2.5.0rc0, 2.5.0rc1)
ERROR: No matching distribution found for tensorflow==1.14

请帮帮忙。

蒂娅。

更新

我尝试通过conda安装tensorflow 1.14,得到了以下错误:

代码语言:javascript
运行
复制
❯ conda install tensorflow==1.14
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: -
Found conflicts! Looking for incompatible packages.
This can take several minutes.  Press CTRL-C to abort.
Examining @/win-64::__cuda==11.1=0:  67%|████████████████████████████████                | 2/3 [00:00<00:00, 13.28it/s]/                                                                                                                       -failed

UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  - tensorflow==1.14 -> python[version='3.6.*|3.7.*']
  - tensorflow==1.14 -> python[version='>=3.6,<3.7.0a0|>=3.7,<3.8.0a0']

Your python: python=3.8

If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.

The following specifications were found to be incompatible with your CUDA driver:

  - feature:/win-64::__cuda==11.1=0
  - feature:|@/win-64::__cuda==11.1=0

Your installed CUDA driver is: 11.1
EN

回答 2

Stack Overflow用户

发布于 2021-08-08 00:05:01

如果您想使用tf.contrib库,使用Tensorflow ==1.14.0或1.15.0,它还需要Python ==3.6/3.7。

票数 2
EN

Stack Overflow用户

发布于 2021-05-11 08:14:53

Tf.contrib在Tensorflow >= 2.0中被删除。一些tf.contrib函数只需使用

代码语言:javascript
运行
复制
import tensorflow.compat.v1 as tf 
tf.disable_v2_behavior() 

为了使用tf.contrib库,可以使用Tensorflow、==1.14或1.15。

正如@hoefling所评论的,Tensorflow 1.14需要Python ==3.6/3.7。

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67244201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档