Train a GAN example difficulty

I am working through the example for using a GAN given here:
https://www.mathworks.com/help/deeplearning/ug/train-generative-adversarial-network.html
And I get an error at the point where it says projectAndReshapeLayer.
Undefined function 'projectAndReshapeLayer' for input arguments of type
'double'.
When I click on the word projectAndReshapeLayer, I get this:
You clicked a link that corresponds to this MATLAB command:
edit(fullfile(matlabroot,'examples','nnet','main','projectAndReshapeLayer.m'))
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
So when I paste this link into the Command Window, it simply creates a blank projectAndReshapeLayer.m file and the error persists.
What am I doing wrong?
To project and reshape the noise input, use the custom layer projectAndReshapeLayer, attached to this example as a supporting file. The projectAndReshapeLayer layer upscales the input using a fully connected operation and reshapes the output to the specified size.
filterSize = 5;
numFilters = 64;
numLatentInputs = 100;
projectionSize = [4 4 512];
layersGenerator = [
imageInputLayer([1 1 numLatentInputs],'Normalization','none','Name','in')
projectAndReshapeLayer(projectionSize,numLatentInputs,'proj');
transposedConv2dLayer(filterSize,4*numFilters,'Name','tconv1')
batchNormalizationLayer('Name','bnorm1')
reluLayer('Name','relu1')
transposedConv2dLayer(filterSize,2*numFilters,'Stride',2,'Cropping','same','Name','tconv2')
batchNormalizationLayer('Name','bnorm2')
reluLayer('Name','relu2')
transposedConv2dLayer(filterSize,numFilters,'Stride',2,'Cropping','same','Name','tconv3')
batchNormalizationLayer('Name','bnorm3')
reluLayer('Name','relu3')
transposedConv2dLayer(filterSize,3,'Stride',2,'Cropping','same','Name','tconv4')
tanhLayer('Name','tanh')];
lgraphGenerator = layerGraph(layersGenerator);

8 comentarios

Dong-Ho
Dong-Ho el 23 de Mayo de 2020
I have also same problem with you !
Anybody tackle it please.
Hi,
Do you stll have this problem? Iy you have, please tell your MATLAB version.
version
I checked this issue under "9.8.0.1380330 (R2020a) Update 2" but can see "projectAndReshapeLayer" correctly.
HTH
Hiroyuki
mark palmer
mark palmer el 3 de Jun. de 2020
Im on 2019b, thanks for the explanation.
SeungRyeol LEE
SeungRyeol LEE el 24 de Jun. de 2020
Use the livescripts not original scripts
Alexander Hagg
Alexander Hagg el 7 de Nov. de 2020
I found that file in the Matlab installation folder
R2020b/examples/nnet/main/projectAndReshapeLayer.m
Yonglei GUI
Yonglei GUI el 21 de Feb. de 2021
classdef projectAndReshapeLayer < nnet.layer.Layer
properties
% (Optional) Layer properties.
OutputSize
end
properties (Learnable)
% Layer learnable parameters.
Weights
Bias
end
methods
function layer = projectAndReshapeLayer(outputSize, numChannels, name)
% Create a projectAndReshapeLayer.
% Set layer name.
layer.Name = name;
% Set layer description.
layer.Description = "Project and reshape layer with output size " + join(string(outputSize));
% Set layer type.
layer.Type = "Project and Reshape";
% Set output size.
layer.OutputSize = outputSize;
% Initialize fully connect weights and bias.
fcSize = prod(outputSize);
layer.Weights = initializeGlorot(fcSize, numChannels);
layer.Bias = zeros(fcSize, 1, 'single');
end
function Z = predict(layer, X)
% Forward input data through the layer at prediction time and
% output the result.
%
% Inputs:
% layer - Layer to forward propagate through
% X - Input data, specified as a 1-by-1-by-C-by-N
% dlarray, where N is the mini-batch size.
% Outputs:
% Z - Output of layer forward function returned as
% an sz(1)-by-sz(2)-by-sz(3)-by-N dlarray,
% where sz is the layer output size and N is
% the mini-batch size.
% Fully connect.
weights = layer.Weights;
bias = layer.Bias;
X = fullyconnect(X,weights,bias,'DataFormat','SSCB');
% Reshape.
outputSize = layer.OutputSize;
Z = reshape(X, outputSize(1), outputSize(2), outputSize(3), []);
end
end
end
function weights = initializeGlorot(numOut, numIn)
% Initialize weights using uniform Glorot.
varWeights = sqrt( 6 / (numIn + numOut) );
weights = varWeights * (2 * rand([numOut, numIn], 'single') - 1);
end
Rahul Gowtham Poola
Rahul Gowtham Poola el 5 de Feb. de 2023
Error using dlnetwork/validateForwardInputs
Layer 'in': Invalid input data. Invalid number of spatial dimensions. Layer expects 2 but received 0.
zahoor m
zahoor m el 12 de Dic. de 2023
Layer 'input': Invalid input data. Invalid number of spatial dimensions. Layer expects 2 but received
0.

Iniciar sesión para comentar.

 Respuesta aceptada

Xiangxue Wang
Xiangxue Wang el 4 de Jun. de 2020
Trying to add the projectAndReshapeLayer path to your matlab searching path. By default, the deep learning example are not in 2020 path.
Hope this will work for you
>> fullfile(matlabroot,'examples','nnet','main','projectAndReshapeLayer.m')
ans =
'C:\Program Files\MATLAB\R2020a\examples\nnet\main\projectAndReshapeLayer.m'
% so adding path to by:
addpath('C:\Program Files\MATLAB\R2020a\examples\nnet\main')

Más respuestas (3)

Ieuan Evans
Ieuan Evans el 25 de Jun. de 2020
Editada: Ieuan Evans el 25 de Jun. de 2020

0 votos

This example was updated in R2020a to use this custom layer. If you use the command openExample('nnet/TrainGenerativeAdversarialNetworkGANExample') in MATLAB, then it will open the correct version of this example for your version of MATLAB.
Hope this helps.
Ramyakrishna
Ramyakrishna el 24 de Oct. de 2022

0 votos

Replace the line with the below line
projectAndReshapeLayer(projectionSize,numLatentInputs,Name='proj')

1 comentario

Dikshya Surabhi
Dikshya Surabhi el 22 de Jul. de 2025
It's not working.. I tried it
projectAndReshapeLayer(projectionSize,numLatentInputs, 'Name', 'proj');
And I still got the error:
'projectAndReshapeLayer' is used in the following examples:
Generate Synthetic Signals Using Conditional GAN
Train Variational Autoencoder (VAE) to Generate Images
Include Custom Layer in Network
Train Generative Adversarial Network (GAN)
Train Wasserstein GAN with Gradient Penalty (WGAN-GP)
Error in cgan (line 27)
projectAndReshapeLayer(projectionSize,numLatentInputs, 'Name', 'proj');

Iniciar sesión para comentar.

Categorías

Más información sobre Install Products en Centro de ayuda y File Exchange.

Productos

Versión

R2019a

Etiquetas

Preguntada:

el 22 de Mayo de 2020

Comentada:

el 22 de Jul. de 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by