Install & Compatibility
Where this runs
tested against v2.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.528s · 128.6MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 4.9s · import 0.519s · 122MB
128MB installed
● package 128MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
dynet
✓ import dynet
✗ import dynet38
The installed package is 'dynet38', but it provides the 'dynet' module internally.
dy
✓ import dynet as dy
Common and recommended alias for the dynet module.
ParameterCollection
✓ from dynet import ParameterCollection
Trainer
✓ from dynet import Trainer
This quickstart demonstrates the core workflow of DyNet: initializing the library, creating a `ParameterCollection` to manage model weights, and defining a function to build a dynamic computation graph for each input. It highlights the crucial `dy.init()` call and `dy.renew_cg()` for managing graph state.
import dynet as dy
import random
# Initialize DyNet (mandatory) - can specify options like --dynet-gpus, --dynet-mem
dy.init()
# Create a model (parameter collection) to hold network weights
m = dy.ParameterCollection()
# Add parameters for a simple feedforward layer
# W: weight matrix (32 output units, 10 input units)
# b: bias vector (32 output units)
# V: output weight matrix (1 output unit, 32 input units)
W = m.add_parameters((32, 10))
b = m.add_parameters(32)
V = m.add_parameters((1, 32))
# Define a function to build the computation graph for a given input
def build_graph(x_val):
# Renew the computation graph for each new example
dy.renew_cg()
# Convert Python list (input vector) to DyNet Expression
x = dy.inputVector(x_val)
# Get parameters as DyNet Expressions for the current graph
W_expr = dy.parameter(W)
b_expr = dy.parameter(b)
V_expr = dy.parameter(V)
# Perform operations to build the graph: tanh activation, logistic output
h = dy.tanh(W_expr * x + b_expr)
o = dy.logistic(V_expr * h)
return o
# Example usage with random input data
input_data = [random.random() for _ in range(10)]
output_expression = build_graph(input_data)
# Get the scalar value from the output expression
output_value = output_expression.value()
print(f"Input (first 5 values): {[f'{v:.2f}' for v in input_data[:5]]}...")
print(f"Predicted Output: {output_value[0]:.4f}")
# For training, you would then get loss, call backward(), and update with a Trainer.
# Example (not run here):
# trainer = dy.SimpleSGDTrainer(m)
# loss = dy.sum_batches(dy.square(output_expression - dy.scalarInput(target_value)))
# loss.backward()
# trainer.update()
Debug
Known issues
gotchaDespite installing `dynet38` via pip, the Python module you need to import is `dynet`. Attempting to `import dynet38` will result in a `ModuleNotFoundError`.fixAlways use `import dynet` or `import dynet as dy` after `pip install dynet38`.
affects: All versions of dynet38
breakingDyNet requires explicit initialization via `dy.init()` before any computation graph operations can occur. Failure to do so will result in a `RuntimeError`.fixEnsure `dy.init()` is called once at the very beginning of your script, typically without arguments unless specific configurations (e.g., GPU, random seed, memory) are needed.
affects: All versions of DyNet/dynet38
gotchaWhen training or performing inference with multiple examples, it's crucial to clear the computation graph for each new input using `dy.renew_cg()`. Failing to do so can lead to memory accumulation, performance degradation, and incorrect gradient computations.fixCall `dy.renew_cg()` at the beginning of any function or loop iteration that constructs a new computation graph for a specific input.
affects: All versions of DyNet/dynet38
gotchaDyNet's performance is highly dependent on optimized BLAS libraries (e.g., MKL, OpenBLAS). While `dynet38` provides pre-built wheels, the performance might vary. For optimal speed, especially on CPU, consider linking against a highly optimized BLAS library.fixFor basic usage, `pip install dynet38` is sufficient. For critical performance applications, investigate building DyNet from source, explicitly linking to MKL or OpenBLAS. Also, consider setting the `DYNET_NUM_THREADS` environment variable.
affects: All versions of DyNet/dynet38
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'dynet38'
The installed package `dynet38` provides the `dynet` module, not `dynet38`.
fixChange your import statement from `import dynet38` to `import dynet` or `import dynet as dy`.
RuntimeError: DyNet is not initialized. Call dy.init() first.
The mandatory `dy.init()` function was not called before attempting any DyNet operations or creating expressions.
fixAdd `import dynet as dy; dy.init()` at the very beginning of your main script or entry point.
Segmentation fault (core dumped) OR Out of memory error during repeated graph computations.
The computation graph is not being reset for each new input, leading to continuous memory allocation and potential overflow.
fixEnsure `dy.renew_cg()` is called at the beginning of each iteration where a new computation graph is constructed (e.g., inside your training loop or prediction function).
Upgrade
Version history
2.2latest on PyPI · released Dec 30, 2025
Audit
Dependencies
No dependency data recorded yet.