Проблема с созданием пользовательского слоя в TensorflowPython

Программы на Python
Anonymous
Проблема с созданием пользовательского слоя в Tensorflow

Сообщение Anonymous »

Когда я пытаюсь создать простую реализацию пользовательского слоя в Tensorflow, я получаю сообщение об ошибке:

Код: Выделить всё

File /opt/anaconda3/lib/python3.11/site-packages/tensorflow/python/util/lazy_loader.py:178 in __getattr__
if item in ("_tfll_mode", "_tfll_initialized", "_tfll_name"):

RecursionError: maximum recursion depth exceeded in comparison
Это мой код:

Код: Выделить всё

import tensorflow as tf

class MyDenseLayer(tf.keras.layers.Layer):
def __init__(self, num_outputs):
super(MyDenseLayer, self).__init__()
self.num_outputs = num_outputs

def build(self, input_shape):
self.kernel = self.add_weight(
"kernel",
shape=[int(input_shape[-1]), self.num_outputs],
initializer='random_normal',  # Initialize with random values
trainable=True
)

def call(self, inputs):
return tf.matmul(inputs, self.kernel)

input_dim = 4  # Number of input features
output_dim = 2  # Match the target data output dimension
layer = MyDenseLayer(output_dim)

# Create a simple model
model = tf.keras.Sequential([
layer,
tf.keras.layers.Dense(2)  # Ensure the final output matches the target
])

# Compile the model
model.compile(optimizer='adam', loss='mse')

# Create random input data
x = tf.random.normal((5, input_dim))  # Batch size of 5
y = tf.random.normal((5, 2))  # Random target data for training

# Train the model
model.fit(x, y, epochs=10)
Помимо компиляции модели, большая часть кода взята из документации по тензорному потоку. Я использую Tensorflow 2.17.0. Заранее спасибо!

Подробнее здесь: https://stackoverflow.com/questions/790 ... tensorflow

Вернуться в «Python»