Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23189

Cast string to int64 is not supported in Keras/Tensorlow

$
0
0

I'm new to keras and tensorflow I want to create a Siamese network using KerasNLP example given here Siamese RoBERTa-networks.My idea is to read the data from a tfrec file using TFRecordDataset. Now this dataset is parsed and ultimately, it returns (entry_lp, exit_lp), match

Where (entry_lp, exit_lp) is the feature and match is the target output.

The get_model() function, takes an input string and then generates a hashed value for that string which will be an integer.Input shape is: (None, 1)Output shape is: (None, 2)

get_siamese_network(model, input_shape) calculates cosine similarity for the two strings. But when running the line: siamese_M.fit(train_data, validation_data=val_data, shuffle=False)

It gives an error: Cast string to int64 is not supportedI looked at few answers here: Cast string to int64 is not supportedbut couldn't figure out why the error is coming, my target value is also an integer type and not string.

It would be helpful to know the cause of this error and suggested solutions that I can try.

import kerasimport tensorflow as tffrom typing import Tuplefrom keras.layers import UnitNormalization, Dotdef get_model():    inputs = keras.Input(shape=(1,), dtype="string")    # Hashing layer for input    hashing = keras.layers.Hashing(num_bins=2**20)    inputs = hashing(inputs)    print("Input shape is: ", inputs.shape)    # Create a constant tensor with value 1    constant_tensor = tf.constant(1, shape=(1,1), dtype=tf.int64)    # Concatenate the input tensor with the constant tensor    output = keras.layers.Concatenate(axis=1)([inputs, constant_tensor])    print("Output shape is: ", output.shape)    model = keras.Model(inputs=inputs, outputs=output)    return modeldef to_xy_data(example) -> Tuple[Tuple[int, int], int]:    # Extracting entry_lp and exit_lp from the example    entry_lp = example['entry_lp']    exit_lp = example['exit_lp']    match = 1 if entry_lp == exit_lp else 0    return (entry_lp, exit_lp), matchdef parse_session(example):    feature_description = {'entry_lp': tf.io.FixedLenFeature([], tf.string),'exit_lp': tf.io.FixedLenFeature([], tf.string)    }    example = tf.io.parse_single_example(example, feature_description)    return exampledef prepare_dataset(tfrec_file):    dataset = tf.data.TFRecordDataset(tfrec_file)    dataset = dataset.map(parse_session)    dataset = dataset.map(to_xy_data)    return datasetdef get_siamese_network(model, input_shape):    input1 = keras.Input(shape=input_shape, dtype="string")    input2 = keras.Input(shape=input_shape, dtype="string")    vector1 = model(input1)    vector2 = model(input2)    # Unit normalization using the UnitNormalization layer    unit_normalized_vector1 = UnitNormalization(axis=-1)(vector1)    unit_normalized_vector2 = UnitNormalization(axis=-1)(vector2)    # Calculate dot product    cosine_similarity_score = Dot(axes=-1)([unit_normalized_vector1, unit_normalized_vector2])    model = keras.Model(inputs=[input1,input2], outputs=cosine_similarity_score)    # compile here with loss    model.compile(loss=keras.losses.MeanSquaredError(), metrics=["accuracy"])    return modeldef train():    M = get_model()    siamese_M = get_siamese_network(M, input_shape=(1,))    sessions_tfrec = "res/data/sessions-2024-04-05-1.tfrec"    # create dataset here    dataset = prepare_dataset(sessions_tfrec)    train_data = dataset.skip(10).batch(32)    val_data = dataset.take(10).batch(32)    siamese_M.fit(train_data, validation_data=val_data, shuffle=False)    # save the model    #M.save("trivial_model.keras")if __name__ == '__main__':    train()

Viewing all articles
Browse latest Browse all 23189

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>