@@ -1207,44 +1207,47 @@ Putting all previous snippet examples together, we obtain the following pipeline
1207
1207
1208
1208
# 1. Define a CEBRA model
1209
1209
cebra_model = cebra.CEBRA(
1210
- model_architecture = "offset10-model",
1211
- batch_size = 512,
1212
- learning_rate = 1e-4,
1213
- temperature_mode='constant',
1214
- temperature = 0.1,
1215
- max_iterations = 10, # TODO(user): to change to ~500-10000 depending on dataset size
1216
- #max_adapt_iterations = 10, # TODO(user): use and to change to ~100-500 if adapting
1217
- time_offsets = 10,
1218
- output_dimension = 8,
1219
- verbose = False
1210
+ model_architecture = "offset10-model",
1211
+ batch_size = 512,
1212
+ learning_rate = 1e-4,
1213
+ temperature_mode='constant',
1214
+ temperature = 0.1,
1215
+ max_iterations = 10, # TODO(user): to change to ~500-10000 depending on dataset size
1216
+ #max_adapt_iterations = 10, # TODO(user): use and to change to ~100-500 if adapting
1217
+ time_offsets = 10,
1218
+ output_dimension = 8,
1219
+ verbose = False
1220
1220
)
1221
-
1221
+
1222
1222
# 2. Load example data
1223
1223
neural_data = cebra.load_data(file="neural_data.npz", key="neural")
1224
1224
new_neural_data = cebra.load_data(file="neural_data.npz", key="new_neural")
1225
1225
continuous_label = cebra.load_data(file="auxiliary_behavior_data.h5", key="auxiliary_variables", columns=["continuous1", "continuous2", "continuous3"])
1226
1226
discrete_label = cebra.load_data(file="auxiliary_behavior_data.h5", key="auxiliary_variables", columns=["discrete"]).flatten()
1227
-
1227
+
1228
+
1228
1229
assert neural_data.shape == (100, 3)
1229
1230
assert new_neural_data.shape == (100, 4)
1230
1231
assert discrete_label.shape == (100, )
1231
1232
assert continuous_label.shape == (100, 3)
1232
-
1233
+
1233
1234
# 3. Split data and labels into train/validation
1234
1235
from sklearn.model_selection import train_test_split
1235
-
1236
- split_idx = int(0.8 * len(neural_data))
1236
+
1237
+ split_idx = int(0.8 * len(neural_data))
1237
1238
# suggestion: 5%-20% depending on your dataset size; note that this splits the
1238
1239
# into an early and late part, which might not be ideal for your data/experiment!
1239
1240
# As a more involved alternative, consider e.g. a nested time-series split.
1240
-
1241
+
1241
1242
train_data = neural_data[:split_idx]
1242
1243
valid_data = neural_data[split_idx:]
1243
-
1244
+
1244
1245
train_continuous_label = continuous_label[:split_idx]
1245
1246
valid_continuous_label = continuous_label[split_idx:]
1246
-
1247
-
1247
+
1248
+ train_discrete_label = discrete_label[:split_idx]
1249
+ valid_discrete_label = discrete_label[split_idx:]
1250
+
1248
1251
# 4. Fit the model
1249
1252
# time contrastive learning
1250
1253
cebra_model.fit(train_data)
@@ -1254,32 +1257,36 @@ Putting all previous snippet examples together, we obtain the following pipeline
1254
1257
cebra_model.fit(train_data, train_continuous_label)
1255
1258
# mixed behavior contrastive learning
1256
1259
cebra_model.fit(train_data, train_discrete_label, train_continuous_label)
1257
-
1260
+
1261
+
1258
1262
# 5. Save the model
1259
1263
tmp_file = Path(tempfile.gettempdir(), 'cebra.pt')
1260
1264
cebra_model.save(tmp_file)
1261
-
1265
+
1262
1266
# 6. Load the model and compute an embedding
1263
1267
cebra_model = cebra.CEBRA.load(tmp_file)
1264
1268
train_embedding = cebra_model.transform(train_data)
1265
1269
valid_embedding = cebra_model.transform(valid_data)
1266
- assert train_embedding.shape == (70, 8) # TODO(user): change to split ratio & output dim
1267
- assert valid_embedding.shape == (30, 8) # TODO(user): change to split ratio & output dim
1268
-
1270
+
1271
+ assert train_embedding.shape == (80, 8) # TODO(user): change to split ratio & output dim
1272
+ assert valid_embedding.shape == (20, 8) # TODO(user): change to split ratio & output dim
1273
+
1269
1274
# 7. Evaluate the model performance (you can also check the train_data)
1270
- goodness_of_fit = cebra.sklearn.metrics.goodness_of_fit_score(cebra_model,
1271
- valid_data,
1275
+ goodness_of_fit = cebra.sklearn.metrics.goodness_of_fit_score(cebra_model,
1276
+ valid_data,
1272
1277
valid_discrete_label,
1273
1278
valid_continuous_label)
1274
-
1279
+
1275
1280
# 8. Adapt the model to a new session
1276
1281
cebra_model.fit(new_neural_data, adapt = True)
1277
-
1282
+
1278
1283
# 9. Decode discrete labels behavior from the embedding
1279
1284
decoder = cebra.KNNDecoder()
1280
1285
decoder.fit(train_embedding, train_discrete_label)
1281
1286
prediction = decoder.predict(valid_embedding)
1282
- assert prediction.shape == (30,)
1287
+ assert prediction.shape == (20,)
1288
+
1289
+
1283
1290
1284
1291
👉 For further guidance on different/customized applications of CEBRA on your own data, refer to the ``examples/ `` folder or to the full documentation folder ``docs/ ``.
1285
1292
0 commit comments