NDArrayViewPtr NDArrayView::DeepClone(bool readOnly/* = false*/) const { NDArrayViewPtr newView(new NDArrayView(this->GetDataType(), this->GetStorageFormat(), this->Shape(), this->Device()), [](_ReferenceCounter* ptr) { delete ptr; }); switch (m_dataType) { case DataType::Float: { auto newMatrix = newView->GetWritableMatrix<float>(); auto thisMatrix = GetMatrix<float>(); newMatrix->AssignValuesOf(*thisMatrix); break; } case DataType::Double: { auto newMatrix = newView->GetWritableMatrix<double>(); auto thisMatrix = GetMatrix<double>(); newMatrix->AssignValuesOf(*thisMatrix); break; } default: LogicError("Unsupported DataType %s", DataTypeName(m_dataType)); break; } newView->m_isReadOnly = readOnly; return NDArrayViewPtr(newView, [](_ReferenceCounter* ptr) { delete ptr; }); }
/*static*/ ValuePtr Value::Create(const NDShape& sampleShape, const std::vector<std::vector<ElementType>>& sequences, const DeviceDescriptor& device, bool readOnly/* = false*/) { size_t sampleSize = sampleShape.TotalSize(); NDMaskPtr deviceValueMask = CreateMask(sampleSize, sequences, device); size_t maxSequenceLength = (deviceValueMask == nullptr) ? sequences[0].size() : deviceValueMask->Shape()[0]; size_t numSequences = sequences.size(); NDShape valueDataShape = sampleShape.AppendShape({ maxSequenceLength, numSequences }); NDArrayViewPtr valueData(new NDArrayView(AsDataType<ElementType>(), valueDataShape, DeviceDescriptor::CPUDevice()), [](ReferenceCount* ptr) { delete ptr; }); ElementType* dataBuffer = valueData->WritableDataBuffer<ElementType>(); for (size_t i = 0; i < numSequences; ++i) std::copy(sequences[i].data(), sequences[i].data() + sequences[i].size(), dataBuffer + (maxSequenceLength * i * sampleSize)); NDArrayViewPtr deviceValueData; if (device == DeviceDescriptor::CPUDevice()) { if (readOnly) deviceValueData = valueData->Alias(true); else deviceValueData = valueData; } else { deviceValueData = NDArrayViewPtr(new NDArrayView(AsDataType<ElementType>(), valueDataShape, device), [](ReferenceCount* ptr) { delete ptr; }); deviceValueData->CopyFrom(*valueData); if (readOnly) deviceValueData = deviceValueData->Alias(true); } return ValuePtr(new Value(deviceValueData, deviceValueMask), [](ReferenceCount* ptr) { delete ptr; }); }
NDArrayViewPtr NDArrayView::RandomUniform(const NDShape& shape, double rangeStart, double rangeEnd, unsigned long seed, const DeviceDescriptor& device/* = DeviceDescriptor::DefaultDevice()*/) { auto matrixDims = GetMatrixDimensions(shape); auto randomUniformMatrix = std::make_shared<Matrix<ElementType>>(Matrix<ElementType>::RandomUniform(matrixDims.first, matrixDims.second, AsCNTKImplDeviceId(device), (ElementType)rangeStart, (ElementType)rangeEnd, seed)); auto tensorView = new TensorView<ElementType>(randomUniformMatrix, AsTensorShape(shape)); auto view = new NDArrayView(AsDataType<ElementType>(), device, StorageFormat::Dense, shape, false, tensorView); return NDArrayViewPtr(view, [](_ReferenceCounter* ptr) { delete ptr; }); }
NDArrayViewPtr NDArrayView::Alias(bool readOnly/* = false*/) const { void* tensorView = nullptr; switch (m_dataType) { case DataType::Float: tensorView = new TensorView<float>(*(GetTensorView<float>())); break; case DataType::Double: tensorView = new TensorView<double>(*(GetTensorView<double>())); break; default: LogicError("Unsupported DataType %s", DataTypeName(m_dataType)); break; } auto aliasView = new NDArrayView(GetDataType(), Device(), GetStorageFormat(), Shape(), IsReadOnly() || readOnly, tensorView);; return NDArrayViewPtr(aliasView, [](_ReferenceCounter* ptr) { delete ptr; }); }