#🔒 Can´t find module named "tensorflow.contrib"

76 messages · Page 1 of 1 (latest)

bright cypress
#

Hello everyone, I have tensorflow installed but can´t import a moduel from it called contrib. I am using Tensorflow 2.17 which doesn´t have contrib module no longer. Whenever i try to uninstall it and reinstall Tensorflow 1.X version, it says that it cant be found because they are no longer supported. I´ve read that only older python versions like 3.7 are compatible with that. Could someone help me downgrade python version and consequently install the Tensorflow version that has that module?

Thank you in advance.

pulsar lodgeBOT
#

@bright cypress

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

thick helm
#

I wanted to answer you earlier but your ticket got closed

bright cypress
#
import numpy as np
from tensorflow.contrib import rnn
from tensorflow.contrib import legacy_seq2seq


class RNNModel:

    def __init__(self,
                 vocabulary_size,
                 batch_size,
                 sequence_length,
                 hidden_layer_size,
                 cells_size,
                 gradient_clip=5.,
                 training=True):

        cells = []
        [cells.append(rnn.LSTMCell(hidden_layer_size)) for _ in range(cells_size)]
        self.cell = rnn.MultiRNNCell(cells)

        self.input_data = tf.placeholder(tf.int32, [batch_size, sequence_length])
        self.targets = tf.placeholder(tf.int32, [batch_size, sequence_length])
        self.initial_state = self.cell.zero_state(batch_size, tf.float32)

        with tf.variable_scope("rnn", reuse=tf.AUTO_REUSE):
            softmax_layer = tf.get_variable("softmax_layer", [hidden_layer_size, vocabulary_size])
            softmax_bias = tf.get_variable("softmax_bias", [vocabulary_size])

        with tf.variable_scope("embedding", reuse=tf.AUTO_REUSE):
            embedding = tf.get_variable("embedding", [vocabulary_size, hidden_layer_size])
            inputs = tf.nn.embedding_lookup(embedding, self.input_data)

        inputs = tf.split(inputs, sequence_length, 1)
        inputs = [tf.squeeze(input_, [1]) for input_ in inputs]

        def loop(previous, _):
            previous = tf.matmul(previous, softmax_layer) + softmax_bias
            previous_symbol = tf.stop_gradient(tf.argmax(previous, 1))
            return tf.nn.embedding_lookup(embedding, previous_symbol)```
#
            outputs, last_state = legacy_seq2seq.rnn_decoder(inputs, self.initial_state, self.cell, loop_function=loop if not training else None)
            output = tf.reshape(tf.concat(outputs, 1), [-1, hidden_layer_size])

        self.logits = tf.matmul(output, softmax_layer) + softmax_bias
        self.probabilities = tf.nn.softmax(self.logits)

        loss = legacy_seq2seq.sequence_loss_by_example([self.logits], [tf.reshape(self.targets, [-1])], [tf.ones([batch_size * sequence_length])])

        with tf.name_scope("cost"):
            self.cost = tf.reduce_sum(loss) / batch_size / sequence_length
        self.final_state = last_state
        self.learning_rate = tf.Variable(0.0, trainable=False)
        trainable_vars = tf.trainable_variables()

        grads, _ = tf.clip_by_global_norm(tf.gradients(self.cost, trainable_vars), gradient_clip)

        with tf.variable_scope("optimizer", reuse=tf.AUTO_REUSE):
            optimizer = tf.train.AdamOptimizer(self.learning_rate)
            self.train_op = optimizer.apply_gradients(zip(grads, trainable_vars))

        tf.summary.histogram("logits", self.logits)
        tf.summary.histogram("probabilitiess", self.probabilities)
        tf.summary.histogram("loss", loss)
        tf.summary.scalar("cost", self.cost)
        tf.summary.scalar("learning_rate", self.learning_rate)

    def sample(self, sess, chars, vocabulary, length):
        state = sess.run(self.cell.zero_state(1, tf.float32))
        text = ""
        char = chars[0]
        for _ in range(length):
            x = np.zeros((1, 1))
            x[0, 0] = vocabulary[char]
            feed = {self.input_data: x, self.initial_state: state}
            [probabilities, state] = sess.run([self.probabilities, self.final_state], feed)
#
            total_sum = np.cumsum(probability)
            sum = np.sum(probability)
            sample = int(np.searchsorted(total_sum, np.random.rand(1) * sum))
            predicted = chars[sample]
            text += predicted
            char = predicted
        return text```
#

this is my code

#

the problem is that rnn is defined in different places, and the thread you specified me tells to search how to manually update the code correct?

#

I have tried to get python 3.7 to work with older versions of tensorflow which had the contrib module, but couldnt do it

#

i guess there might be easier ways but i just dont know how

thick helm
#

Thread tells you to install older version of tenserflow

bright cypress
#

yea, but whenever i try that on CMD i get an error saying that there are no versions older than TensorFlow 2.X

#

all the 1.X versions are no longer possible to install by "pip install tensorflow==1.14"

thick helm
#

what os are you on?

bright cypress
#

windows11

thick helm
#

1 sec

bright cypress
#

Sure.

thick helm
#

maybe download them manually?

bright cypress
#

sure, do i need to uninstall mine first?

#

like "pip uninstall tensorflow"?

thick helm
#

yeah

#

*ping on reply

bright cypress
# thick helm *ping on reply

One unrelated question. I tried to manually install python 3.7 and then after not working, i tried to go back to the version i had 3.11.9 but couldnt transition to it by using a CMD command so I just intalled it again manually as well. Somehow i lost all my packages that I had, but somehow VSCode still knows that i have tensorflow somewhere.

#

btw, i installed through the link you sent but no package is showing on the print as you can see

thick helm
#

how did you remove python?

#

don't tell me I removed it's folder

bright cypress
#

i didn´t

#

i just installed the python 3.7 version and inserted on CMD "py3.7 .... something"

#

and it changed to that version when i ran "python -V"

thick helm
#

3.7 still exists

bright cypress
#

oh, and I checked a box on the installation talking about PATH

thick helm
#

You have 2 different versions

#

change your iterpretator in VSCode

#

you probably would have to create new venv

#

There are 2 types of interpretators: global (works on your system all the time) and local (works only inside your project)

#

I hope you can distinguish between them, right?

bright cypress
#

yea

#

I might have changed my global but im still using the old one as the local

#

Is there a way to get the 3.7 version on local and try to install there the old tensorflow version?

thick helm
#

wait I just realised, did you install tenserflow in your global interpreter?

#

you need to install it using cmd in your VSCode

bright cypress
thick helm
#

So it installs on your local interpreter

bright cypress
#

ahahah in fact

bright cypress
#

to get my python 3.7 in there

thick helm
#

I use venv most of the time

#

conda is for testing your app on many python versions

bright cypress
#

gotcha

#

i clicked venv, how do i get python 3.7 in there now? sry, i´ve never done this

thick helm
#

you didn't add 3.7 to PATH

bright cypress
#

do i have to install it again and check the add to PATH box?

thick helm
#

yes

bright cypress
#

gotcha

thick helm
#

Remove old one first

bright cypress
#

i´ve already selected python 3.7.0 as the env

thick helm
#

I forgot

#

Right click > Open in local terminal

bright cypress
thick helm
#

install new version

#

Make sure it's a LOCAL interpreter

bright cypress
#

Worked

#

On the arrow on the right side, i clicked and the option "open command" is there.

#

I also had the wrong version of python 3.7.0 installed, which was not compatible with tensorflow 1.14

bright cypress
pulsar lodgeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

🔒 Can´t find module named "tensorflow.contrib"