Код: Выделить всё
Invalid dtype: complex64Почему моя модель в керасе не знает dtype complex?
Минимальный пример:
Код: Выделить всё
import tensorflow as tf
import numpy as np
from tensorflow.keras import layers, models
#generate random complex valued data with dimension (N,hdim)
N = 1000
hdim = 64
batch_size = 32
epochs = 10
X = np.random.randn(N,hdim) + 1j*np.random.randn(N,hdim)
y = np.random.randn(N,1)
#create a tensorflow dataset
train_dataset = tf.data.Dataset.from_tensor_slices((X,y))
train_dataset = train_dataset.shuffle(buffer_size=len(X)).batch(batch_size)
#Some custom layers
class input_manip(tf.keras.layers.Layer):
def call(self, inputs):
in_real = tf.math.real(inputs)
in_imag = tf.math.imag(inputs)
return in_real, in_imag
class output_manip(tf.keras.layers.Layer):
def call(self, in_real, in_imag):
output = tf.reduce_sum(tf.square(in_real) + tf.square(in_imag))
return output
# Define the model
def ftlm_model(hdim):
# Input layer
inputs = layers.Input(shape=(hdim,), dtype=tf.complex64)
in_man = input_manip()
in_real, in_imag = in_man(inputs)
print(in_real.shape, in_real.dtype)
shared_dense = layers.Dense(hdim, activation='relu') # Shared dense layer
dense_real = shared_dense(in_real) # Dense layer for real part
dense_imag = shared_dense(in_imag) # Dense layer for imaginary part
out_man = output_manip()
output = out_man(dense_real, dense_imag)
#Output layer combining the real and imaginary parts
# Define the model
model = models.Model(inputs=inputs, outputs=output)
return model
model = ftlm_model(hdim)
#Print a summary of the model
model.summary()
class CustomMSELoss(tf.keras.losses.Loss):
def __init__(self):
super(CustomMSELoss, self).__init__()
def call(self, y_true, y_pred):
# Custom loss calculation (Weighted Mean Squared Error)
y_pred = tf.math.real(y_pred)
loss = tf.reduce_mean(tf.square(y_true - y_pred))
return loss
custom_loss = CustomMSELoss()
# Compile the model
model.compile(optimizer='adam', loss=custom_loss)
# Train the model
history = model.fit(train_dataset, epochs=epochs)
Мне интересно, это просто случайная удача, что мой код работает на моей машине, или это что-то с tensorflow 2.17?
Если это невозможно — как распределить веса между двумя моделями, если мне приходится разделять вне модели?
Я пытался выполнить разделение вне модели, но потом делаю это не знаю, как распределить вес для некоторых слоев.
Подробнее здесь: https://stackoverflow.com/questions/790 ... red-layers