[Python] AutoKeras need to rewrite Keras code in python

Discussão em 'Python' iniciado por Stack, Março 2, 2025.

  1. Stack

    Stack Membro Participativo

    i have trained data with Autokeras and save the model.

    import pandas as pd

    from numpy import asarray

    from pandas import read_csv
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import LabelEncoder
    from autokeras import StructuredDataClassifier



    import tensorflow as tf
    print(tf.__version__)
    print(tf.config.list_physical_devices())
    url="C:\\Users\\borsa\\Downloads\\turkish-spacy-models-main\\tr_core_news_trf\\1045__4.csv"
    # load the dataset
    # dataset = loadtxt(idd1, delimiter=',')
    # dataset=dataset.fillna(dataset.mean())
    # split into input (X) and output (y) variables
    # X = dataset[:,1:66]
    # y = dataset[:,0]

    import numpy as np
    np.object = np.object_


    # titanic_data = loadtxt(idd1, delimiter=',')
    titanic_data = pd.read_csv(url, sep=',')


    # Drop rows with missing target values
    # titanic_data = titanic_data.dropna(subset=['t'])
    # titanic_data=titanic_data.fillna(titanic_data.mean())

    # X = titanic_data[['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10', 'a11', 'a12', 'a13', 'a14', 'a15', 'a16', 'a17', 'a18', 'a19', 'a20', 'a21', 'a22', 'a23', 'a24', 'a25', 'a26', 'a27', 'a28', 'a29', 'a30', 'a31', 'a32', 'a33','a34', 'a35', 'a36', 'a37', 'a38', 'a39', 'a40', 'a41', 'a42', 'a43', 'a44', 'a45', 'a46', 'a47', 'a48', 'a49', 'a50', 'a51', 'a52', 'a53', 'a54', 'a55', 'a56', 'a57', 'a58', 'a59','a60', 'a61', 'a62','a63' ,'a64', 'a65', 'a66','a67' ]]
    # X = titanic_data[[ 'a2', 'a3', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10', 'a11', 'a12', 'a13', 'a14', 'a15', 'a16', 'a17', 'a18', 'a19', 'a20', 'a21', 'a22', 'a23', 'a24', 'a25', 'a26', 'a27', 'a28', 'a29', 'a30', 'a31', 'a32', 'a33','a34', 'a35', 'a36', 'a37', 'a38', 'a39', 'a40', 'a41', 'a42', 'a43', 'a44', 'a45', 'a46', 'a47', 'a48', 'a49', 'a50', 'a51', 'a52', 'a53', 'a54', 'a55', 'a56', 'a57', 'a58', 'a59', 'a60', 'a61', 'a62', 'a63', 'a64', 'a65', 'a66', 'a67', 'a68', 'a69', 'a70', 'a71', 'a72' ]]
    X = titanic_data[[ 'a2', 'a3', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10', 'a11', 'a12', 'a13', 'a14', 'a15', 'a16', 'a17', 'a18', 'a19', 'a20', 'a21', 'a22', 'a23', 'a24', 'a25', 'a26', 'a27', 'a28', 'a29', 'a30', 'a31', 'a32', 'a33','a34', 'a35', 'a36', 'a37', 'a38', 'a39', 'a40', 'a41', 'a42', 'a43', 'a44', 'a45', 'a46', 'a47', 'a48', 'a49', 'a50', 'a51', 'a52', 'a53', 'a54', 'a55', 'a56', 'a57', 'a58', 'a59', 'a60', 'a61', 'a62', 'a63', 'a64', 'a65', 'a66', 'a67', 'a68', 'a69', 'a70', 'a71', 'a72' ]]

    # titanic_data.head(4)

    y = titanic_data['t']

    target_col = "t"
    # X = titanic_data.loc[:, titanic_data.columns != target_col]
    # y = titanic_data.loc[:, target_col]



    # data = titanic_data.values
    # X, y = data[:, :-1], data[:, -1]
    # titanic_data = pd.get_dummies(titanic_data, drop_first=True)

    # Split the data into features and labels
    # X = titanic_data.drop('t', axis=1)
    # y = titanic_data['t']

    # X= titanic_data.iloc[:,:-1]
    # y= titanic_data.iloc[:,-1:]
    # y = y.values.reshape(-1, 1)
    print(f"Train data: {X.shape}\n\
    Train labels: {y.shape}")

    # define the keras model
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.02, random_state=1)
    # y_train = y_train.reshape(-1, 1)
    # y_test = y_test.reshape(-1, 1)
    print(X.shape, y.shape)
    # basic data preparation

    # X = X.astype('float32')
    # y = LabelEncoder().fit_transform(y)
    # separate into train and test sets
    # X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)
    print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
    # define the search
    search = StructuredDataClassifier(max_trials=40,overwrite=True)
    # perform the search
    search.fit(X_train, y_train,epochs=1, verbose=1)
    # evaluate the model
    loss, acc = search.evaluate(X_test, y_test, verbose=1)
    print('Accuracy: %.3f' % acc)


    # Step 2: Export the underlying Keras model
    keras_model = search.export_model()

    # Step 3: Save the Keras model
    keras_model.save('my_autokeras_model', save_format='tf')
    keras_model.summary()


    and get model summary

    Layer (type) Output Shape Param #
    ================================================================= input_1 (InputLayer) [(None, 70)] 0
    multi_category_encoding (M (None, 70) 0 ultiCategoryEncoding)
    normalization (Normalizati (None, 70) 141 on)

    dense (Dense) (None, 512) 36352
    re_lu (ReLU) (None, 512) 0
    dense_1 (Dense) (None, 512) 262656
    re_lu_1 (ReLU) (None, 512) 0
    dense_2 (Dense) (None, 512) 262656
    re_lu_2 (ReLU) (None, 512) 0
    dense_3 (Dense) (None, 8) 4104
    classification_head_1 (Sof (None, 8) 0 tmax)

    ================================================================= Total params: 565909 (2.16 MB) Trainable params: 565768 (2.16 MB) Non-trainable params: 141 (568.00 Byte)
    ___________________________________________________________


    İ want to write the code in Keras.. Can you help pls When i try to pass normalization layer to Dense layer i got error...

    TypeError: You are passing KerasTensor(type_spec=TensorSpec(shape=(None, 70), dtype=tf.float32, name=None), name='normalization_9/truediv:0', description="created by layer 'normalization_9'"), an intermediate Keras symbolic input/output, to a TF API that does not allow registering custom dispatchers, such as `tf.cond`, `tf.function`, gradient tapes, or `tf.map_fn`.
    Keras Functional model construction only supports TF API calls that *do* support dispatching, such as `tf.math.add` or `tf.reshape`.
    Other APIs cannot be called directly on symbolic Kerasinputs/outputs.
    You can work around this limitation by putting the operation in a custom Keras layer `call` and calling that layer on this symbolic input/output.

    Continue reading...

Compartilhe esta Página