Upload 3 files
Browse files- __init__.py +77 -0
- model.py +17 -0
- weights.h5 +3 -0
__init__.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file was created by
|
| 2 |
+
# MATLAB Deep Learning Toolbox Converter for TensorFlow Models.
|
| 3 |
+
# 13-Aug-2025 21:15:39
|
| 4 |
+
|
| 5 |
+
import TextStoryAI.model
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
def load_model(load_weights=True, debug=False):
|
| 9 |
+
m = model.create_model()
|
| 10 |
+
if load_weights:
|
| 11 |
+
loadWeights(m, debug=debug)
|
| 12 |
+
return m
|
| 13 |
+
|
| 14 |
+
## Utility functions:
|
| 15 |
+
|
| 16 |
+
import tensorflow as tf
|
| 17 |
+
import h5py
|
| 18 |
+
|
| 19 |
+
def loadWeights(model, filename=os.path.join(__package__, "weights.h5"), debug=False):
|
| 20 |
+
with h5py.File(filename, 'r') as f:
|
| 21 |
+
# Every layer is an h5 group. Ignore non-groups (such as /0)
|
| 22 |
+
for g in f:
|
| 23 |
+
if isinstance(f[g], h5py.Group):
|
| 24 |
+
group = f[g]
|
| 25 |
+
layerName = group.attrs['Name']
|
| 26 |
+
numVars = int(group.attrs['NumVars'])
|
| 27 |
+
if debug:
|
| 28 |
+
print("layerName:", layerName)
|
| 29 |
+
print(" numVars:", numVars)
|
| 30 |
+
# Find the layer index from its namevar
|
| 31 |
+
layerIdx = layerNum(model, layerName)
|
| 32 |
+
layer = model.layers[layerIdx]
|
| 33 |
+
if debug:
|
| 34 |
+
print(" layerIdx=", layerIdx)
|
| 35 |
+
# Every weight is an h5 dataset in the layer group. Read the weights
|
| 36 |
+
# into a list in the correct order
|
| 37 |
+
weightList = [0]*numVars
|
| 38 |
+
for d in group:
|
| 39 |
+
dataset = group[d]
|
| 40 |
+
varName = dataset.attrs['Name']
|
| 41 |
+
shp = intList(dataset.attrs['Shape'])
|
| 42 |
+
weightNum = int(dataset.attrs['WeightNum'])
|
| 43 |
+
# Read the weight and put it into the right position in the list
|
| 44 |
+
if debug:
|
| 45 |
+
print(" varName:", varName)
|
| 46 |
+
print(" shp:", shp)
|
| 47 |
+
print(" weightNum:", weightNum)
|
| 48 |
+
weightList[weightNum] = tf.constant(dataset[()], shape=shp)
|
| 49 |
+
# Assign the weights into the layer
|
| 50 |
+
for w in range(numVars):
|
| 51 |
+
if debug:
|
| 52 |
+
print("Copying variable of shape:")
|
| 53 |
+
print(weightList[w].shape)
|
| 54 |
+
layer.variables[w].assign(weightList[w])
|
| 55 |
+
if debug:
|
| 56 |
+
print("Assignment successful.")
|
| 57 |
+
print("Set variable value:")
|
| 58 |
+
print(layer.variables[w])
|
| 59 |
+
# Finalize layer state
|
| 60 |
+
if hasattr(layer, 'finalize_state'):
|
| 61 |
+
layer.finalize_state()
|
| 62 |
+
|
| 63 |
+
def layerNum(model, layerName):
|
| 64 |
+
# Returns the index to the layer
|
| 65 |
+
layers = model.layers
|
| 66 |
+
for i in range(len(layers)):
|
| 67 |
+
if layerName==layers[i].name:
|
| 68 |
+
return i
|
| 69 |
+
print("")
|
| 70 |
+
print("WEIGHT LOADING FAILED. MODEL DOES NOT CONTAIN LAYER WITH NAME: ", layerName)
|
| 71 |
+
print("")
|
| 72 |
+
return -1
|
| 73 |
+
|
| 74 |
+
def intList(myList):
|
| 75 |
+
# Converts a list of numbers into a list of ints.
|
| 76 |
+
return list(map(int, myList))
|
| 77 |
+
|
model.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file was created by
|
| 2 |
+
# MATLAB Deep Learning Toolbox Converter for TensorFlow Models.
|
| 3 |
+
# 13-Aug-2025 21:15:39
|
| 4 |
+
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
from tensorflow import keras
|
| 7 |
+
from tensorflow.keras import layers
|
| 8 |
+
|
| 9 |
+
def create_model():
|
| 10 |
+
input = keras.Input(shape=(None,1894))
|
| 11 |
+
input_lstm_input = input
|
| 12 |
+
lstm = layers.LSTM(128, name='lstm_', activation='tanh', recurrent_activation='sigmoid', return_sequences=False, return_state=False)(input_lstm_input)
|
| 13 |
+
fc = layers.Dense(1894, name="fc_")(lstm)
|
| 14 |
+
softmax = layers.Softmax()(fc)
|
| 15 |
+
|
| 16 |
+
model = keras.Model(inputs=[input], outputs=[softmax])
|
| 17 |
+
return model
|
weights.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1afcb1e8a2d8f2f5523975906cd00c3766561ed9b1dc4e0324b779341e34ce6a
|
| 3 |
+
size 5168200
|