From 9e947edadd9bf1bc289d65e39d170695f4b41e4a Mon Sep 17 00:00:00 2001 From: yks72p <29257108+yks72p@users.noreply.github.com> Date: Sat, 5 Nov 2022 16:25:02 +0300 Subject: [PATCH] fixed circular imports in init.py and autograd.py --- python/needle/__init__.py | 2 +- python/needle/autograd.py | 5 +++-- python/needle/init.py | 24 +++++++++++------------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/python/needle/__init__.py b/python/needle/__init__.py index caf370b..082c15f 100644 --- a/python/needle/__init__.py +++ b/python/needle/__init__.py @@ -1,7 +1,7 @@ +from . import init from .autograd import Tensor, cpu, all_devices from . import ops from .ops import * -from . import init from . import data from . import nn from . import optim diff --git a/python/needle/autograd.py b/python/needle/autograd.py index 8053501..ea0a3ad 100644 --- a/python/needle/autograd.py +++ b/python/needle/autograd.py @@ -1,9 +1,10 @@ """Core data structures.""" import needle -from typing import List, Optional, NamedTuple, Tuple, Union +from typing import List, Optional, NamedTuple, Tuple, Union, Dict from collections import namedtuple import numpy -from needle import init + +from . import init # needle version LAZY_MODE = False diff --git a/python/needle/init.py b/python/needle/init.py index a35f5b7..96ee299 100644 --- a/python/needle/init.py +++ b/python/needle/init.py @@ -1,27 +1,25 @@ -import math -import needle as ndl - +from .autograd import Tensor, cpu def rand(*shape, low=0.0, high=1.0, device=None, dtype="float32", requires_grad=False): """ Generate random numbers uniform between low and high """ - device = ndl.cpu() if device is None else device + device = cpu() if device is None else device array = device.rand(*shape) * (high - low) + low - return ndl.Tensor(array, device=device, dtype=dtype, requires_grad=requires_grad) + return Tensor(array, device=device, dtype=dtype, requires_grad=requires_grad) def randn(*shape, mean=0.0, std=1.0, device=None, dtype="float32", requires_grad=False): """ Generate random normal with specified mean and std deviation """ - device = ndl.cpu() if device is None else device + device = cpu() if device is None else device array = device.randn(*shape) * std + mean - return ndl.Tensor(array, device=device, dtype=dtype, requires_grad=requires_grad) + return Tensor(array, device=device, dtype=dtype, requires_grad=requires_grad) def constant(*shape, c=1.0, device=None, dtype="float32", requires_grad=False): """ Generate constant Tensor """ - device = ndl.cpu() if device is None else device + device = cpu() if device is None else device array = device.ones(*shape, dtype=dtype) * c # note: can change dtype - return ndl.Tensor(array, device=device, dtype=dtype, requires_grad=requires_grad) + return Tensor(array, device=device, dtype=dtype, requires_grad=requires_grad) def ones(*shape, device=None, dtype="float32", requires_grad=False): @@ -36,15 +34,15 @@ def zeros(*shape, device=None, dtype="float32", requires_grad=False): def randb(*shape, p=0.5, device=None, dtype="bool", requires_grad=False): """ Generate binary random Tensor """ - device = ndl.cpu() if device is None else device + device = cpu() if device is None else device array = device.rand(*shape) <= p - return ndl.Tensor(array, device=device, dtype=dtype, requires_grad=requires_grad) + return Tensor(array, device=device, dtype=dtype, requires_grad=requires_grad) def one_hot(n, i, device=None, dtype="float32", requires_grad=False): """ Generate one-hot encoding Tensor """ - device = ndl.cpu() if device is None else device - return ndl.Tensor(device.one_hot(n,i.numpy(), dtype=dtype), device=device, requires_grad=requires_grad) + device = cpu() if device is None else device + return Tensor(device.one_hot(n,i.numpy(), dtype=dtype), device=device, requires_grad=requires_grad) def xavier_uniform(fan_in, fan_out, gain=1.0, **kwargs):