Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kears import error with torch backend #18955

Closed
JacksonZhu90 opened this issue Dec 17, 2023 · 4 comments
Closed

Kears import error with torch backend #18955

JacksonZhu90 opened this issue Dec 17, 2023 · 4 comments

Comments

@JacksonZhu90
Copy link

Hi,

I was trying to use keras 3 with torch. But when I try to import keras, it shows "No module named 'tensorflow'". I do not use tensorflow here. The torch version I am using is 2.1.2

import numpy as np
import os
os.environ["KERAS_BACKEND"] = "torch"
import torch
import matplotlib.pyplot as plt
import keras

ModuleNotFoundError Traceback (most recent call last)
Cell In[12], line 1
----> 1 import keras

File D:\ProgramData\Anaconda3\envs\pytorch2\Lib\site-packages\keras_init_.py:8
1 """DO NOT EDIT.
2
3 This file was autogenerated. Do not edit it by hand,
4 since your modifications would be overwritten.
5 """
----> 8 from keras import _tf_keras
9 from keras import activations
10 from keras import applications

File D:\ProgramData\Anaconda3\envs\pytorch2\Lib\site-packages\keras_tf_keras_init_.py:1
----> 1 from keras._tf_keras import keras

File D:\ProgramData\Anaconda3\envs\pytorch2\Lib\site-packages\keras_tf_keras\keras_init_.py:8
1 """DO NOT EDIT.
2
3 This file was autogenerated. Do not edit it by hand,
4 since your modifications would be overwritten.
5 """
----> 8 from keras import activations
9 from keras import applications
10 from keras import callbacks

File D:\ProgramData\Anaconda3\envs\pytorch2\Lib\site-packages\keras\activations_init_.py:8
1 """DO NOT EDIT.
2
3 This file was autogenerated. Do not edit it by hand,
4 since your modifications would be overwritten.
5 """
----> 8 from keras.src.activations import deserialize
9 from keras.src.activations import get
10 from keras.src.activations import serialize

File D:\ProgramData\Anaconda3\envs\pytorch2\Lib\site-packages\keras\src_init_.py:1
----> 1 from keras.src import activations
2 from keras.src import applications
3 from keras.src import backend

File D:\ProgramData\Anaconda3\envs\pytorch2\Lib\site-packages\keras\src\activations_init_.py:3
1 import types
----> 3 from keras.src.activations.activations import elu
4 from keras.src.activations.activations import exponential
5 from keras.src.activations.activations import gelu

File D:\ProgramData\Anaconda3\envs\pytorch2\Lib\site-packages\keras\src\activations\activations.py:1
----> 1 from keras.src import backend
2 from keras.src import ops
3 from keras.src.api_export import keras_export

File D:\ProgramData\Anaconda3\envs\pytorch2\Lib\site-packages\keras\src\backend_init_.py:33
31 # Import backend functions.
32 if backend() == "tensorflow":
---> 33 from keras.src.backend.tensorflow import * # noqa: F403
34 elif backend() == "jax":
35 from keras.src.backend.jax import * # noqa: F403

File D:\ProgramData\Anaconda3\envs\pytorch2\Lib\site-packages\keras\src\backend\tensorflow_init_.py:1
----> 1 from keras.src.backend.tensorflow import core
2 from keras.src.backend.tensorflow import distribution_lib
3 from keras.src.backend.tensorflow import image

File D:\ProgramData\Anaconda3\envs\pytorch2\Lib\site-packages\keras\src\backend\tensorflow\core.py:4
1 import types
3 import numpy as np
----> 4 import tensorflow as tf
5 from tensorflow.compiler.tf2xla.python.xla import dynamic_update_slice
7 from keras.src.backend.common import KerasVariable

ModuleNotFoundError: No module named 'tensorflow'

@sachinprasadhs
Copy link
Collaborator

Hi,

I was able to run the code without any error,
Make sure you install the latest Keras and torch with

!pip install -U keras
!pip install -U torch

Below import is working fine.

import numpy as np
import os
os.environ["KERAS_BACKEND"] = "torch"
import torch
import matplotlib.pyplot as plt
import keras

Attaching the Gist here for reference.

Copy link

github-actions bot commented Jan 2, 2024

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

@github-actions github-actions bot added the stale label Jan 2, 2024
Copy link

This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.

Copy link

Are you satisfied with the resolution of your issue?
Yes
No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants