From e7e059f9b13b4183ca67e65204a598de2c7b6ace Mon Sep 17 00:00:00 2001 From: shove70 Date: Tue, 21 Nov 2023 12:30:56 +0800 Subject: [PATCH] set nil as the initial value of the struct reference type fields --- autograd/gates_basic.v | 8 ++++---- autograd/gates_blas.v | 4 ++-- autograd/gates_exp.v | 2 +- autograd/gates_pow.v | 4 ++-- autograd/gates_trig.v | 6 +++--- autograd/node.v | 2 +- autograd/payload.v | 2 +- autograd/variable.v | 6 +++--- datasets/imdb.v | 8 ++++---- datasets/mnist.v | 8 ++++---- nn/gates/activation/elu.v | 2 +- nn/gates/activation/leaky_relu.v | 2 +- nn/gates/activation/relu.v | 2 +- nn/gates/activation/sigmoid.v | 2 +- nn/gates/layers/dropout.v | 2 +- nn/gates/layers/flatten.v | 2 +- nn/gates/layers/linear.v | 6 +++--- nn/gates/layers/maxpool.v | 2 +- nn/gates/loss/mse.v | 4 ++-- nn/gates/loss/sigmoid_cross_entropy.v | 4 ++-- nn/gates/loss/softmax_cross_entropy.v | 4 ++-- nn/layers/linear.v | 4 ++-- nn/models/sequential.v | 2 +- nn/models/sequential_info.v | 2 +- src/iter.v | 2 +- src/iter_axis.v | 2 +- src/tensor.v | 2 +- src/tensor_vcl_d_vcl.v | 2 +- 28 files changed, 49 insertions(+), 49 deletions(-) diff --git a/autograd/gates_basic.v b/autograd/gates_basic.v index 3c42b5d0..0a7df1b4 100644 --- a/autograd/gates_basic.v +++ b/autograd/gates_basic.v @@ -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] { @@ -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] { diff --git a/autograd/gates_blas.v b/autograd/gates_blas.v index cbba448e..fae14868 100644 --- a/autograd/gates_blas.v +++ b/autograd/gates_blas.v @@ -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] { diff --git a/autograd/gates_exp.v b/autograd/gates_exp.v index 5dab0670..47033f53 100644 --- a/autograd/gates_exp.v +++ b/autograd/gates_exp.v @@ -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] { diff --git a/autograd/gates_pow.v b/autograd/gates_pow.v index 8b74fab5..68ee1cb6 100644 --- a/autograd/gates_pow.v +++ b/autograd/gates_pow.v @@ -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] { diff --git a/autograd/gates_trig.v b/autograd/gates_trig.v index 436b408d..df7112e8 100644 --- a/autograd/gates_trig.v +++ b/autograd/gates_trig.v @@ -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] { @@ -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] { @@ -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] { diff --git a/autograd/node.v b/autograd/node.v index 6ed0be88..27480761 100644 --- a/autograd/node.v +++ b/autograd/node.v @@ -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 } diff --git a/autograd/payload.v b/autograd/payload.v index 903c2d3d..02bdbf9a 100644 --- a/autograd/payload.v +++ b/autograd/payload.v @@ -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] { diff --git a/autograd/variable.v b/autograd/variable.v index 43350790..fe19e1ae 100644 --- a/autograd/variable.v +++ b/autograd/variable.v @@ -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 diff --git a/datasets/imdb.v b/datasets/imdb.v index f4820003..204413a3 100644 --- a/datasets/imdb.v +++ b/datasets/imdb.v @@ -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. diff --git a/datasets/mnist.v b/datasets/mnist.v index a10810bf..9e16cf26 100644 --- a/datasets/mnist.v +++ b/datasets/mnist.v @@ -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. diff --git a/nn/gates/activation/elu.v b/nn/gates/activation/elu.v index 6219b01a..b1ef2922 100644 --- a/nn/gates/activation/elu.v +++ b/nn/gates/activation/elu.v @@ -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] { diff --git a/nn/gates/activation/leaky_relu.v b/nn/gates/activation/leaky_relu.v index 8e010a6f..b6117343 100644 --- a/nn/gates/activation/leaky_relu.v +++ b/nn/gates/activation/leaky_relu.v @@ -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] { diff --git a/nn/gates/activation/relu.v b/nn/gates/activation/relu.v index f56907f3..e8fb9e99 100644 --- a/nn/gates/activation/relu.v +++ b/nn/gates/activation/relu.v @@ -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] { diff --git a/nn/gates/activation/sigmoid.v b/nn/gates/activation/sigmoid.v index 7794a6dc..7562cdef 100644 --- a/nn/gates/activation/sigmoid.v +++ b/nn/gates/activation/sigmoid.v @@ -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] { diff --git a/nn/gates/layers/dropout.v b/nn/gates/layers/dropout.v index e91198ce..e223c82d 100644 --- a/nn/gates/layers/dropout.v +++ b/nn/gates/layers/dropout.v @@ -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] { diff --git a/nn/gates/layers/flatten.v b/nn/gates/layers/flatten.v index a3f09bd6..3d522fb0 100644 --- a/nn/gates/layers/flatten.v +++ b/nn/gates/layers/flatten.v @@ -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 } diff --git a/nn/gates/layers/linear.v b/nn/gates/layers/linear.v index 5c67a6c7..62aa8f0f 100644 --- a/nn/gates/layers/linear.v +++ b/nn/gates/layers/linear.v @@ -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] { diff --git a/nn/gates/layers/maxpool.v b/nn/gates/layers/maxpool.v index 0b003ce9..601a5e46 100644 --- a/nn/gates/layers/maxpool.v +++ b/nn/gates/layers/maxpool.v @@ -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 diff --git a/nn/gates/loss/mse.v b/nn/gates/loss/mse.v index 5a9f7040..d355e717 100644 --- a/nn/gates/loss/mse.v +++ b/nn/gates/loss/mse.v @@ -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] { diff --git a/nn/gates/loss/sigmoid_cross_entropy.v b/nn/gates/loss/sigmoid_cross_entropy.v index e7786982..382f1158 100644 --- a/nn/gates/loss/sigmoid_cross_entropy.v +++ b/nn/gates/loss/sigmoid_cross_entropy.v @@ -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] { diff --git a/nn/gates/loss/softmax_cross_entropy.v b/nn/gates/loss/softmax_cross_entropy.v index 841894d8..dd3d7c81 100644 --- a/nn/gates/loss/softmax_cross_entropy.v +++ b/nn/gates/loss/softmax_cross_entropy.v @@ -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] { diff --git a/nn/layers/linear.v b/nn/layers/linear.v index 6b35a0fa..8f92a161 100644 --- a/nn/layers/linear.v +++ b/nn/layers/linear.v @@ -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] { diff --git a/nn/models/sequential.v b/nn/models/sequential.v index 98885199..9dbd577b 100644 --- a/nn/models/sequential.v +++ b/nn/models/sequential.v @@ -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. diff --git a/nn/models/sequential_info.v b/nn/models/sequential_info.v index 8b0a0dd3..a3150a16 100644 --- a/nn/models/sequential_info.v +++ b/nn/models/sequential_info.v @@ -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 diff --git a/src/iter.v b/src/iter.v index d5e47831..50e51989 100644 --- a/src/iter.v +++ b/src/iter.v @@ -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 diff --git a/src/iter_axis.v b/src/iter_axis.v index c0744db2..dd5eab2a 100644 --- a/src/iter_axis.v +++ b/src/iter_axis.v @@ -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 diff --git a/src/tensor.v b/src/tensor.v index de6456fa..5f568bd3 100644 --- a/src/tensor.v +++ b/src/tensor.v @@ -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 diff --git a/src/tensor_vcl_d_vcl.v b/src/tensor_vcl_d_vcl.v index ff008d85..da5bd286 100644 --- a/src/tensor_vcl_d_vcl.v +++ b/src/tensor_vcl_d_vcl.v @@ -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