60 std::shared_ptr<BackwardFunction> gradFunction;
61 std::shared_ptr<Tensor> grad;
91 template<
typename... Args>
92 explicit Tensor(Args... args) :
Tensor(std::vector<size_t>({
static_cast<size_t>(args)...})) {
105 [[nodiscard]] std::shared_ptr<Tensor> copy()
const;
144 template<
typename... Args>
146 std::vector<size_t> index = std::vector<size_t>({
static_cast<size_t>(args)...});
151 return data[effectiveIndex];
175 [[nodiscard]]
size_t findEffectiveAddress(
const std::vector<size_t>& index,
size_t depth)
const;
182 void verifyIndex(
const std::vector<size_t>& index)
const;
185 typedef std::shared_ptr<Tensor> sTensor;
196 sTensor sum(
const sTensor& a);
198 void fill(
float value, sTensor& tensor);
199 void fill(
const sTensor& value, sTensor& tensor);
201 sTensor add(
const sTensor& a,
const sTensor& b);
203 sTensor subtract(
const sTensor& a,
const sTensor& b);
205 sTensor hadamard(
const sTensor& a,
const sTensor& b);
207 sTensor divide(
const sTensor& a,
const sTensor& b);
209 sTensor log(
const sTensor& a);
211 sTensor multiply(
const sTensor& a,
float constant);
213 sTensor multiply(
const sTensor& a,
const sTensor& b);
215 sTensor transpose(
const sTensor& a);
217 sTensor relu(
const sTensor& a);
219 sTensor sigmoid(
const sTensor& a);
222 sTensor sum(
const sTensor& a);
224 sTensor add(
const sTensor& a,
const sTensor& b);
226 sTensor subtract(
const sTensor& a,
const sTensor& b);
228 sTensor hadamard(
const sTensor& a,
const sTensor& b);
230 sTensor divide(
const sTensor& a,
const sTensor& b);
232 sTensor log(
const sTensor& a);
234 sTensor multiply(
const sTensor& a,
float constant);
236 sTensor multiply(
const sTensor& a,
const sTensor& b);
238 sTensor transpose(
const sTensor& a);
240 sTensor relu(
const sTensor& a);
242 sTensor sigmoid(
const sTensor& a);
Header file to declare common functions regarding memory allocation on host.
DataLocation
Enumerate to specify where data is located.
Definition: cache.h:24
Definition: functions.h:20
Contains information about the current session.
Definition: session.cuh:19
Class to represent multidimensional arrays.
Definition: tensor.h:26
std::vector< size_t > shape
Store the shape of the tensor.
Definition: tensor.h:32
void verifyIndex(const std::vector< size_t > &index) const
Verify that an index of an element is within the shape of the tensor.
Definition: tensor.cpp:158
Tensor()
Initialize an empty tensor.
Definition: tensor.cpp:23
size_t findEffectiveAddress(const std::vector< size_t > &index, size_t depth) const
Finds the address of an element in the flattened data array given its index in non-flattened tensor.
Definition: tensor.cpp:150
~Tensor()
The destructor.
Definition: tensor.cpp:107
void move(DataLocation target)
Move the tensor to the designated destination.
Definition: tensor.cpp:87
static Tensor construct1d(const std::vector< float > &data)
Static method to easily initialize a 1D tensor with given data.
Definition: tensor.cpp:122
DataLocation location
The location of the tensor.
Definition: tensor.h:47
size_t size
Store the total size of the tensor.
Definition: tensor.h:40
Tensor(Args... args)
Construct a tensor based on the passed shape.
Definition: tensor.h:92
Tensor & operator=(const Tensor &other)
The assignment operator.
Definition: tensor.cpp:64
Session session
Session object containing information about current session.
Definition: tensor.h:57
void computeSize()
Compute the total size of the tensor based on its shape.
Definition: tensor.cpp:115
static Tensor construct2d(const std::vector< std::vector< float >> &data)
Static method to easily initialize a 2D tensor with given data.
Definition: tensor.cpp:131
float * data
The data stored by the tensor.
Definition: tensor.h:52
float & operator()(Args... args)
Method to access an element of the tensor at a specific method.
Definition: tensor.h:145
Header file declaring the Session class.
std::ostream & operator<<(std::ostream &stream, const Tensor &tensor)
Enables the tensor to be printed using std::cout.
Definition: tensor.cpp:253