tensorflow scope lost in Jupyter lab - python

I'm having trouble splitting up tensorflow code that runs fine in 1 cell in jupyter lab (v2.1.5). As a simple test I did this:
import tensorflow as tf
a=2
print(a,tf.__version__)
result:
2 2.3.0-rc0
cell2:
print(a)
print(tf.__version__)
result:
2
AttributeError Traceback (most recent call last)
<ipython-input-2-d9c2549c3ca0> in <module>
1 print(a)
----> 2 print(tf.__version__)
AttributeError: 'NoneType' object has no attribute '__version__'
Any toughts what's missing?

Related

module 'tensorflow' has no attribute 'variable' in google Colaboratory

I'm trying to create a variable tensor in google Collaboratory with this code, but error because TensorFlow doesn't have a module about variables. what's the solution?
import tensorflow as tf
var_1 = tf.variable(tf.ones([2,3]))
Error:
AttributeError Traceback (most recent call last)
<ipython-input-2-2666f49bf801> in <module>()
----> 1 var_1 = tf.variable(tf.ones([2,3]))```
AttributeError: module 'tensorflow' has no attribute 'variable'
There is a typo error. Use capital letter V for Variable.
You can refer below code for the same.
import tensorflow as tf
var_1 = tf.Variable(tf.ones([2,3]))
var_1
Click here, for more details

"AttributeError: module 'tensorflow' has no attribute 'tables_initializer' " using TFv2.2.0, Why and how to solve it?

I got errors when I want to run these lines.
import tensorflow.python.keras.backend as K
session = K.get_session()
init_op = tf.group(tf.tables_initializer(),tf.global_variables_initializer(),
tf.local_variables_initializer())
session.run(init_op)
np.random.seed(1)
tf.set_random_seed(1)
The error says: module 'tensorflow' has no attribute 'tables_initializer', and in the same form happens with global_variables_initializer, and local_variables_initializer (when I run them individually).
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-40-82a30eae4406> in <module>()
5
6 session = K.get_session()
----> 7 init_op = tf.group(tf.tables_initializer(),tf.global_variables_initializer(),
tf.local_variables_initializer())
8 session.run(init_op)
9 np.random.seed(1)
AttributeError: module 'tensorflow' has no attribute 'tables_initializer'
Please, help...
Given code is compatible with Tensorflow 1.x.
To make it work with Tensorflow 2.x, need some modification to your code, i.e.,change the library names as
tf.compat.v1.tables_initializer()
tf.compat.v1.global_variables_initializer()
tf.compat.v1.local_variables_initializer().
Find the working code snippet
import tensorflow.compat.v1 as tf
import tensorflow.python.keras.backend as K
tf.compat.v1.disable_eager_execution()
import numpy as np
session = K.get_session()
init_op = tf.group(tf.tables_initializer(),tf.global_variables_initializer(),
tf.local_variables_initializer())
session.run(init_op)
np.random.seed(1)
tf.set_random_seed(1)

Import module has no attribute

I'm using Python since few days and I'm trying to learn as much as possible from it. I'm using Jupyter notebook as well. I made a python file fibo.py where I code a function fib and I saved it. In the same folder I try to import that module and use the function fib
import pandas as pd
import numpy as np
import fibo
result = fibo.fib(10)
but I get the following error message :
AttributeError: module 'fibo' has no attribute 'fib'
Could you please give me some suggestions where the problem should be? Thank you in advance.
This is the code for fibo.py
# Fibonacci numbers module\n",
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
The error I get is here:
AttributeError Traceback (most recent call last)
<ipython-input-1-73202afd3146> in <module>
3
4 import fibo
----> 5 result = fibo.fib(10)
AttributeError: module 'fibo' has no attribute 'fib'
I tried also
from fibo import fib
And I get the following:
ImportError Traceback (most recent call last)
<ipython-input-2-b2d78eaf1dcb> in <module>
4 import fibo
5 #result = fibo.fib(10)
----> 6 from fibo import fib
ImportError: cannot import name 'fib' from 'fibo' (C:\Users\my_folder\Documents\JupyterWork\fibo.py)
Save the file as .py using any text editor other than jupyter notebook.
Because jupyter notebook file are stored in JSON format.

how to fix TypeError: 'NoneType' object has no attribute '__getitem__'

I am new to opencv with tensorflow ,I have a simple hand gesture recognition notebook that am trying to run on my jupyter . After running the kernel the windows load for less than two minutes then i get an error
TypeError Traceback (most recent call last)
<ipython-input-10-2396a75050b8> in <module>()
----> 8 roi=vidimg[0:224,0:224]
TypeError: 'NoneType' object has no attribute '__getitem__'
How do i solve this
using MacOS
#full code
import cv2
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import load_model
import os
handornot_model=load_model("handornot.h5")
hand_model=load_model("handgesture.h5")
def predict_image(image):
return(hand_model.predict(image))
def predict_handornot(image):
return(handornot_model.predict(image))
k=0
cap=cv2.VideoCapture(0)
while True:
_,vidimg=cap.read()
cv2.rectangle(vidimg,(0,0),(224,224),(225,0,0),4)
roi=vidimg[0:224,0:224]
cv2.imwrite("tempdata//{}.jpg".format(k),roi)
img=cv2.imread("tempdata//{}.jpg".format(k))
img=img[:,:,::-1]
img2=img.reshape((1,img.shape[0],img.shape[1],img.shape[2]))
handornot=predict_handornot(img2)
result=str(np.argmax(handornot))
if result=="0":
writeonimg="Please show your hand"
else:
result2=predict_image(img2)
writeonimg=str(np.argmax(result)+1)
os.remove("tempdata//{}.jpg".format(k))
k+=1
cv2.putText(vidimg,writeonimg,(110,250),cv2.FONT_HERSHEY_COMPLEX,1,
(225,225,0),2)
cv2.imshow("vidimg",vidimg)
cv2.imshow("roi",roi)
if cv2.waitKey(2) & 0xFF==ord("q"):
break
cap.release()
cv2.destroyAllWindows()

Importing & using HoltWinters from hardPredictions

I'm stuck on importing a module from hardPredictions. I can find documentation on installing the libraryand using the module module, but not how to import the module.
I've tried the following:
from hardPredictions import HoltWinters
model = HoltWinters()
This gives me an error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-72e6928695cf> in <module>
----> 1 model = HoltWinters()
TypeError: 'module' object is not callable
This works, but then I can't pass my ts to the model:
from hardPredictions import HoltWinters
model = HoltWinters
model.fit(ts)
gives an error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-13-f40ff401465a> in <module>
1 model = HoltWinters
----> 2 model.fit(ts)
AttributeError: module 'hardPredictions.HoltWinters' has no attribute 'fit'
I've also tried running help() and dir() on HoltWinters, but think the main thing is I'm lacking the skill to ask the library how to import a model. Your advice is much appreciated!

Categories

Resources