DocsCode Generation
Code Generation
Export your visual model to production-ready Python code in multiple frameworks.
Supported Frameworks
| Framework | Output | Best For |
|---|---|---|
| PyTorch | torch.nn.Module | Research, flexibility |
| TensorFlow | tf.keras.Model | Production, deployment |
| Keras | keras.Model | Simplicity, readability |
| PyCyxWiz | pycyxwiz.Sequential | CyxWiz native training |
Generating Code
Toolbar Buttons
Click the framework button in the Node Editor toolbar:
[Save] [Load] [PyTorch] [TensorFlow] [Keras] [Validate]
Nodes Menu
Nodes > Generate Code > [Framework]Context Menu
Right-click on canvas > Generate Code > [Framework]
Output Location
Generated code is automatically inserted into the Script Editor.
PyTorch Output
import torch
import torch.nn as nn
class CyxWizModel(nn.Module):
def __init__(self):
super(CyxWizModel, self).__init__()
self.dense_0 = nn.Linear(784, 128)
self.relu_1 = nn.ReLU()
self.dense_2 = nn.Linear(128, 64)
self.relu_3 = nn.ReLU()
self.dense_4 = nn.Linear(64, 10)
self.softmax_5 = nn.Softmax(dim=1)
def forward(self, x):
x = self.dense_0(x)
x = self.relu_1(x)
x = self.dense_2(x)
x = self.relu_3(x)
x = self.dense_4(x)
x = self.softmax_5(x)
return x
# Instantiate model
model = CyxWizModel()
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)TensorFlow Output
import tensorflow as tf
from tensorflow.keras import layers, Model
class CyxWizModel(Model):
def __init__(self):
super(CyxWizModel, self).__init__()
self.dense_0 = layers.Dense(128, activation=None)
self.relu_1 = layers.ReLU()
self.dense_2 = layers.Dense(64, activation=None)
self.relu_3 = layers.ReLU()
self.dense_4 = layers.Dense(10, activation=None)
self.softmax_5 = layers.Softmax()
def call(self, x, training=False):
x = self.dense_0(x)
x = self.relu_1(x)
x = self.dense_2(x)
x = self.relu_3(x)
x = self.dense_4(x)
x = self.softmax_5(x)
return x
# Instantiate and compile
model = CyxWizModel()
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy']
)Keras Output
Sequential API
import keras
from keras import layers
model = keras.Sequential([
layers.Dense(128,
activation='relu',
input_shape=(784,)),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax'),
])
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)Functional API
import keras
from keras import layers
inputs = keras.Input(shape=(784,))
x = layers.Dense(128)(inputs)
x = layers.ReLU()(x)
x = layers.Dense(64)(x)
x = layers.ReLU()(x)
outputs = layers.Dense(10,
activation='softmax')(x)
model = keras.Model(
inputs=inputs,
outputs=outputs
)PyCyxWiz Output
import pycyxwiz as cyx
# Initialize backend
cyx.initialize()
# Build model
model = cyx.Sequential()
model.add(cyx.Dense(128, activation='relu'))
model.add(cyx.Dense(64, activation='relu'))
model.add(cyx.Dense(10, activation='softmax'))
# Configure training
model.compile(
optimizer=cyx.Adam(lr=0.001),
loss=cyx.CrossEntropyLoss(),
metrics=['accuracy']
)
# Train
model.fit(
x_train, y_train,
epochs=10,
batch_size=32,
validation_data=(x_val, y_val)
)Handling Skip Connections
PyTorch
def forward(self, x):
identity = x
x = self.conv(x)
x = self.bn(x)
x = x + identity # Residual
x = self.relu(x)
return xTensorFlow/Keras
identity = x x = conv(x) x = bn(x) x = layers.Add()([x, identity]) x = layers.ReLU()(x)
Validation Before Generation
The code generator validates the graph:
- Has Input - DatasetInput node present
- Has Output - Output node present
- No Cycles - Acyclic graph
- All Connected - No orphan nodes
- Type Compatibility - Pin types match
Naming Convention
| Node Type | Naming Pattern | Example |
|---|---|---|
| Layers | {type}_{index} | dense_0, conv_1 |
| Custom names | User-specified | Preserved |
| Skip connections | identity_{index} | identity_0 |
Tips
- Validate First - Fix errors before generating
- Review Output - Check generated code for correctness
- Test Generated Code - Run to verify functionality
- Customize as Needed - Generated code is a starting point
- Keep Graph Simple - Complex graphs = complex code
Exporting to File
From Script Editor
- Generate code to Script Editor
- File > Save As
- Choose
.pyextension
Direct Export
Nodes > Export Code to File
Saves to file with framework in filename:
- model_pytorch.py
- model_tensorflow.py
- model_keras.py