Dear all,
I want to create an User Defined Function (UDF) in Excel (using xlwings) to handle TensorFlow models in Python. I installed Python, tensorflow and xlwings and created the following Python script:
# Importe as bibliotecas necessárias
import numpy as np
import xlwings as xw
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
#Função para criar e treinar uma rede neural
@xw.func
def treinar_rede():
# Defina os dados de treinamento (exemplo simplificado)
x_train = np.array([1, 2, 3, 4])
y_train = np.array([3, 5, 7, 9])
# Expanda as dimensões dos dados de entrada
x_train = np.expand_dims(x_train, axis=-1)
y_train = np.expand_dims(y_train, axis=-1)
# Crie o modelo da rede neural
model = keras.Sequential([
layers.Dense(10, activation='relu'), # Camada oculta com 10 neurônios e função de ativação ReLU
layers.Dense(1), # Camada de saída com 1 neurônio
])
# Compile o modelo
model.compile(optimizer='adam', loss='mean_squared_error')
# Treine o modelo
model.fit(x_train, y_train, epochs=10, verbose=0)
# Salvando o modelo
model.save('meu_modelo.keras')
return "modelo treinado!"
Then, I created the following VBA code in Excel with the same name of Python script:
Sub RunNeuralNetwork()
Range("A1").Value = ""
Range("A1").Value = treinar_rede()
End Sub
So, when I execute the Macro, I receive the text “modelo treinado!” in cell A1 as expected but, when I look in the directory of spreadsheet (which is the same of the Python script), the model “meu_modelo.keras” doesn’t appear. What is wrong?