Skip to content

Commit

Permalink
set nil as the initial value of the struct reference type fields (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
shove70 authored Nov 21, 2023
1 parent e191e63 commit edf4fc8
Show file tree
Hide file tree
Showing 28 changed files with 49 additions and 49 deletions.
8 changes: 4 additions & 4 deletions autograd/gates_basic.v
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ pub fn (g &SubstractGate[T]) cache[T](mut result Variable[T], args ...CacheParam

pub struct MultiplyGate[T] {
pub:
a &Variable[T]
b &Variable[T]
a &Variable[T] = unsafe { nil }
b &Variable[T] = unsafe { nil }
}

pub fn multiply_gate[T](a &Variable[T], b &Variable[T]) &MultiplyGate[T] {
Expand Down Expand Up @@ -119,8 +119,8 @@ pub fn (g &MultiplyGate[T]) cache[T](mut result Variable[T], args ...CacheParam)

pub struct DivideGate[T] {
pub:
a &Variable[T]
b &Variable[T]
a &Variable[T] = unsafe { nil }
b &Variable[T] = unsafe { nil }
}

pub fn divide_gate[T](a &Variable[T], b &Variable[T]) &DivideGate[T] {
Expand Down
4 changes: 2 additions & 2 deletions autograd/gates_blas.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import vtl.la

pub struct MatMulGate[T] {
pub:
a &Variable[T]
b &Variable[T]
a &Variable[T] = unsafe { nil }
b &Variable[T] = unsafe { nil }
}

pub fn matmul_gate[T](a &Variable[T], b &Variable[T]) &MatMulGate[T] {
Expand Down
2 changes: 1 addition & 1 deletion autograd/gates_exp.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import vtl

pub struct ExpGate[T] {
pub:
a &Variable[T]
a &Variable[T] = unsafe { nil }
}

pub fn exp_gate[T](a &Variable[T]) &ExpGate[T] {
Expand Down
4 changes: 2 additions & 2 deletions autograd/gates_pow.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import math
import vtl

pub struct PowGate[T] {
a &Variable[T]
b &Variable[T]
a &Variable[T] = unsafe { nil }
b &Variable[T] = unsafe { nil }
}

pub fn pow_gate[T](a &Variable[T], b &Variable[T]) &PowGate[T] {
Expand Down
6 changes: 3 additions & 3 deletions autograd/gates_trig.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import vtl

pub struct SinGate[T] {
pub:
a &Variable[T]
a &Variable[T] = unsafe { nil }
}

pub fn sin_gate[T](a &Variable[T]) &SinGate[T] {
Expand Down Expand Up @@ -37,7 +37,7 @@ pub fn (g &SinGate[T]) cache[T](mut result Variable[T], args ...CacheParam) ! {

pub struct CosGate[T] {
pub:
a &Variable[T]
a &Variable[T] = unsafe { nil }
}

pub fn cos_gate[T](a &Variable[T]) &CosGate[T] {
Expand Down Expand Up @@ -70,7 +70,7 @@ pub fn (g &CosGate[T]) cache[T](mut result Variable[T], args ...CacheParam) ! {

pub struct TanGate[T] {
pub:
a &Variable[T]
a &Variable[T] = unsafe { nil }
}

pub fn tan_gate[T](a &Variable[T]) &TanGate[T] {
Expand Down
2 changes: 1 addition & 1 deletion autograd/node.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mut:
// The variables that created this node
parents []&Variable[T]
// Wrapper around a Tensor, contains operation data
payload &Payload[T]
payload &Payload[T] = unsafe { nil }
// Debug use only, contains a name for a node
name string
}
Expand Down
2 changes: 1 addition & 1 deletion autograd/payload.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module autograd
pub struct Payload[T] {
pub:
// Contents of the paylod
variable &Variable[T]
variable &Variable[T] = unsafe { nil }
}

pub fn payload[T](variable &Variable[T]) &Payload[T] {
Expand Down
6 changes: 3 additions & 3 deletions autograd/variable.v
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ pub mut:
// The value of the Variable. This should not be edited outside
// of Variable operations, as other edits will not be tracked
// and will lead to incorrect results
value &vtl.Tensor[T]
value &vtl.Tensor[T] = unsafe { nil }
// The graph the variable is associated with. This is a reference,
// as a variable does not own its context
context &Context[T]
context &Context[T] = unsafe { nil }
// The gradient of the Variable. This is set as a reference to
// the value of a Variable unless `backprop` has been called, in
// which case all related Variables will have their gradient
// updated correctly
grad &vtl.Tensor[T]
grad &vtl.Tensor[T] = unsafe { nil }
// If set to true, this variable will track its operations,
// otherwise it will act similar to a vtl.Tensor, only calculating
// forward operations
Expand Down
8 changes: 4 additions & 4 deletions datasets/imdb.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ pub const (
// ImdbDataset is a dataset for sentiment analysis.
pub struct ImdbDataset {
pub:
train_features &vtl.Tensor[string]
train_labels &vtl.Tensor[int]
test_features &vtl.Tensor[string]
test_labels &vtl.Tensor[int]
train_features &vtl.Tensor[string] = unsafe { nil }
train_labels &vtl.Tensor[int] = unsafe { nil }
test_features &vtl.Tensor[string] = unsafe { nil }
test_labels &vtl.Tensor[int] = unsafe { nil }
}

// load_imdb_helper loads the IMDB dataset for a given split.
Expand Down
8 changes: 4 additions & 4 deletions datasets/mnist.v
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ pub const (
// MnistDataset is a dataset of MNIST handwritten digits.
pub struct MnistDataset {
pub:
train_features &vtl.Tensor[u8]
train_labels &vtl.Tensor[u8]
test_features &vtl.Tensor[u8]
test_labels &vtl.Tensor[u8]
train_features &vtl.Tensor[u8] = unsafe { nil }
train_labels &vtl.Tensor[u8] = unsafe { nil }
test_features &vtl.Tensor[u8] = unsafe { nil }
test_labels &vtl.Tensor[u8] = unsafe { nil }
}

// load_mnist_helper loads the MNIST dataset from the given filename.
Expand Down
2 changes: 1 addition & 1 deletion nn/gates/activation/elu.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import vtl.nn.internal

pub struct EluGate[T] {
pub:
cache &vtl.Tensor[T]
cache &vtl.Tensor[T] = unsafe { nil }
}

pub fn elu_gate[T](cache &vtl.Tensor[T]) &EluGate[T] {
Expand Down
2 changes: 1 addition & 1 deletion nn/gates/activation/leaky_relu.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import vtl.nn.internal

pub struct LeakyReluGate[T] {
pub:
cache &vtl.Tensor[T]
cache &vtl.Tensor[T] = unsafe { nil }
}

pub fn leaky_relu_gate[T](cache &vtl.Tensor[T]) &LeakyReluGate[T] {
Expand Down
2 changes: 1 addition & 1 deletion nn/gates/activation/relu.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import vtl.nn.internal

pub struct ReLUGate[T] {
pub:
cache &vtl.Tensor[T]
cache &vtl.Tensor[T] = unsafe { nil }
}

pub fn relu_gate[T](cache &vtl.Tensor[T]) &ReLUGate[T] {
Expand Down
2 changes: 1 addition & 1 deletion nn/gates/activation/sigmoid.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import vtl.nn.internal

pub struct SigmoidGate[T] {
pub:
cache &vtl.Tensor[T]
cache &vtl.Tensor[T] = unsafe { nil }
}

pub fn sigmoid_gate[T](cache &vtl.Tensor[T]) &SigmoidGate[T] {
Expand Down
2 changes: 1 addition & 1 deletion nn/gates/layers/dropout.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import vtl.autograd
pub struct DropoutGate[T] {
pub:
prob f64
mask &vtl.Tensor[T]
mask &vtl.Tensor[T] = unsafe { nil }
}

pub fn dropout_gate[T](mask &vtl.Tensor[T], prob f64) &DropoutGate[T] {
Expand Down
2 changes: 1 addition & 1 deletion nn/gates/layers/flatten.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import vtl.autograd

pub struct FlattenGate[T] {
pub:
input &autograd.Variable[T]
input &autograd.Variable[T] = unsafe { nil }
cached_shape []int
}

Expand Down
6 changes: 3 additions & 3 deletions nn/gates/layers/linear.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import vtl.stats

pub struct LinearGate[T] {
pub:
input &autograd.Variable[T]
weight &autograd.Variable[T]
bias &autograd.Variable[T]
input &autograd.Variable[T] = unsafe { nil }
weight &autograd.Variable[T] = unsafe { nil }
bias &autograd.Variable[T] = unsafe { nil }
}

pub fn linear_gate[T](input &autograd.Variable[T], weight &autograd.Variable[T], bias &autograd.Variable[T]) &LinearGate[T] {
Expand Down
2 changes: 1 addition & 1 deletion nn/gates/layers/maxpool.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import vtl.nn.internal

pub struct MaxPool2DGate[T] {
pub:
max_indices &vtl.Tensor[int]
max_indices &vtl.Tensor[int] = unsafe { nil }
kernel []int
shape []int
stride []int
Expand Down
4 changes: 2 additions & 2 deletions nn/gates/loss/mse.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import vtl.nn.internal

pub struct MseGate[T] {
pub:
cache &autograd.Variable[T]
target &vtl.Tensor[T]
cache &autograd.Variable[T] = unsafe { nil }
target &vtl.Tensor[T] = unsafe { nil }
}

pub fn mse_gate[T](cache &autograd.Variable[T], target &vtl.Tensor[T]) &MseGate[T] {
Expand Down
4 changes: 2 additions & 2 deletions nn/gates/loss/sigmoid_cross_entropy.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import vtl.nn.internal

pub struct SigmoidCrossEntropyGate[T] {
pub:
cache &autograd.Variable[T]
target &vtl.Tensor[T]
cache &autograd.Variable[T] = unsafe { nil }
target &vtl.Tensor[T] = unsafe { nil }
}

pub fn sigmoid_cross_entropy_gate[T](cache &autograd.Variable[T], target &vtl.Tensor[T]) &SigmoidCrossEntropyGate[T] {
Expand Down
4 changes: 2 additions & 2 deletions nn/gates/loss/softmax_cross_entropy.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import vtl.nn.internal

pub struct SoftmaxCrossEntropyGate[T] {
pub:
cache &autograd.Variable[T]
target &vtl.Tensor[T]
cache &autograd.Variable[T] = unsafe { nil }
target &vtl.Tensor[T] = unsafe { nil }
}

pub fn softmax_cross_entropy_gate[T](cache &autograd.Variable[T], target &vtl.Tensor[T]) &SoftmaxCrossEntropyGate[T] {
Expand Down
4 changes: 2 additions & 2 deletions nn/layers/linear.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import vtl.nn.types

// LinearLayer is a layer that applies a linear transformation to its input.
pub struct LinearLayer[T] {
weights &autograd.Variable[T]
bias &autograd.Variable[T]
weights &autograd.Variable[T] = unsafe { nil }
bias &autograd.Variable[T] = unsafe { nil }
}

pub fn linear_layer[T](ctx &autograd.Context[T], input_dim int, output_dim int) types.Layer[T] {
Expand Down
2 changes: 1 addition & 1 deletion nn/models/sequential.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn init() {

pub struct Sequential[T] {
pub mut:
info &SequentialInfo[T]
info &SequentialInfo[T] = unsafe { nil }
}

// sequential creates a new sequential network with a new context.
Expand Down
2 changes: 1 addition & 1 deletion nn/models/sequential_info.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import vtl.nn.loss
import vtl.nn.types

pub struct SequentialInfo[T] {
ctx &autograd.Context[T]
ctx &autograd.Context[T] = unsafe { nil }
pub mut:
layers []types.Layer[T]
loss types.Loss
Expand Down
2 changes: 1 addition & 1 deletion src/iter.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum IteratorStrategy {
@[heap]
pub struct TensorIterator[T] {
pub:
tensor &Tensor[T]
tensor &Tensor[T] = unsafe { nil }
next_handler IteratorStrategy
pub mut:
iteration int
Expand Down
2 changes: 1 addition & 1 deletion src/iter_axis.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module vtl
@[heap]
pub struct TensorAxisIterator[T] {
pub:
tensor &Tensor[T]
tensor &Tensor[T] = unsafe { nil }
pub mut:
shape []int
strides []int
Expand Down
2 changes: 1 addition & 1 deletion src/tensor.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum MemoryFormat {
@[heap]
pub struct Tensor[T] {
pub mut:
data &storage.CpuStorage[T]
data &storage.CpuStorage[T] = unsafe { nil }
memory MemoryFormat
size int
shape []int
Expand Down
2 changes: 1 addition & 1 deletion src/tensor_vcl_d_vcl.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import vtl.storage
@[heap]
pub struct VclTensor[T] {
pub mut:
data &storage.VclStorage[T]
data &storage.VclStorage[T] = unsafe { nil }
memory MemoryFormat
size int
shape []int
Expand Down

0 comments on commit edf4fc8

Please sign in to comment.