使用keras时出错:模块'keras.layers'没有属性'TextVectorization'

问题描述 投票:0回答:1
import os

os.environ["KERAS_BACKEND"] = "tensorflow"

import pandas as pd
import pathlib
import random
import string
import re
import numpy as np
import tensorflow as tf
import keras
from keras import layers

MAX_SEQUENCE_LENGTH = 40
INP_VOCAB_SIZE = 15000

input_vectorization = keras.layers.TextVectorization(
    max_tokens=INP_VOCAB_SIZE,
    output_mode="int",
    output_sequence_length=MAX_SEQUENCE_LENGTH
)

AttributeError: module 'keras.layers' has no attribute 'TextVectorization'

我导入 keras 模块并从 keras 获取图层,但是当我尝试使用 keras.layers.TextVectorization 时,我的代码会抛出该错误。

我尝试以不同的方式编写调用,但随后我得到了一个新的 AttributeError。

input_vectorization = tf.keras.TextVectorization(
    #same inside as before
)

AttributeError: module 'keras' has no attribute '__version__'

我还尝试使用 pip 重新安装tensorflow和keras,但这并没有改变任何东西。我该怎么办?

python tensorflow keras keras-layer
1个回答
0
投票

在tensorflow 2.0之后,每当您使用tensorflow时,您都应该使用tf.keras访问keras,并且Textvectorization是tf.keras的一部分,而不是基础keras。根据文档 - https://www.tensorflow.org/api_docs/python/tf/keras/layers/TextVectorization

因此您只需将通话更改为

tf.keras.layers.TextVectorization

© www.soinside.com 2019 - 2024. All rights reserved.