RuntimeError: Given groups=1, weight of size [128, 1, 3, 3], expected input[1, 64, 10, 446] to have 1 channels, but got 64 channels instead

Arupreza: <p>I tried to add an early stop but it’s not running, please help me silver this error.</p>
<p>CreatData = []<br>
X = Concatnated[:, :446]<br>
y = Concatnated[:,446]<br>
strat_time = 0<br>
timestamp = 10<br>
length = len(Concatnated)</p>
<p>for i in range(int(abs(length/10-1))):<br>
CreatData.append((X[strat_time:timestamp, :446], y[timestamp]))</p>
<pre><code>strat_time = strat_time + 10
timestamp = timestamp + 10
</code></pre>
<p>train_dataset, test_dataset = train_test_split(CreatData, test_size=0.2)<br>
print("Number of Training Sequences: ", len(train_dataset))<br>
print("Number of Testing Sequences: ", len(test_dataset))</p>
<p>def set_seed(seed=0):<br>
np.random.seed(seed)<br>
random.seed(seed)<br>
torch.manual_seed(seed)<br>
torch.cuda.manual_seed(seed)<br>
torch.cuda.manual_seed_all(seed)<br>
torch.backends.cudnn.deterministic = True<br>
torch.backends.cudnn.benchmark = False<br>
os.environ[‘PYTHONHASHSEED’] = str(seed)</p>
<p>set_seed(0)</p>
<p>class ConvNet(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(ConvNet, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(in_channels = 1, out_channels = 128, kernel_size = 3)<br>
self.pool = nn.MaxPool2d(2,2)<br>
self.conv2 = nn.Conv2d(in_channels = 128, out_channels = 64, kernel_size = 3)</p>
<pre><code>    self.fc1 = nn.Linear(in_features = 64*1*110, out_features = 120)
    self.fc2 = nn.Linear(in_features = 120, out_features = 84)
    self.out = nn.Linear(in_features = 84, out_features = 4)

def forward(self, x):
    x = self.pool(F.relu(self.conv1(x)))  
    x = self.pool(F.relu(self.conv2(x)))  
    x = x.view(-1, 64*1*110)           
    x = F.relu(self.fc1(x))               
    x = F.relu(self.fc2(x))               
    x = self.out(x)                      
    return x
</code></pre>
<p>class EarlyStopping:<br>
def <strong>init</strong>(self, mode, path, patience=3, delta=0):<br>
if mode not in {‘min’, ‘max’}:<br>
raise ValueError(“Argument mode must be one of ‘min’ or ‘max’.”)<br>
if patience &lt;= 0:<br>
raise ValueError(“Argument patience must be a postive integer.”)<br>
if delta &lt; 0:<br>
raise ValueError(“Argument delta must not be a negative number.”)</p>
<pre><code>    self.mode = mode
    self.patience = patience
    self.delta = delta
    self.path = path
    self.best_score = np.inf if mode == 'min' else -np.inf
    self.counter = 0
    
def _is_improvement(self, val_score):
    """Return True iff val_score is better than self.best_score."""
    if self.mode == 'max' and val_score &gt; self.best_score + self.delta:
        return True
    elif self.mode == 'min' and val_score &lt; self.best_score - self.delta:
        return True
    return False
    
def __call__(self, val_score, model):
    """Return True iff self.counter &gt;= self.patience.
    """
    
    if self._is_improvement(val_score):
        self.best_score = val_score
        self.counter = 0
        torch.save(model.state_dict(), self.path)
        print('Val loss improved. Saved model.')
        return False
    else:
        self.counter += 1
        print(f'Early stopping counter: {self.counter}/{self.patience}')
        if self.counter &gt;= self.patience:
            print(f'Stopped early. Best val loss: {self.best_score:.4f}')
            return True
</code></pre>
<p>def train_one_epoch(model, train_loader, optimizer, device, criterion):<br>
“”“Train model for one epoch and return the mean train_loss.”“”<br>
model.train()<br>
running_loss_train = 0<br>
for inputs, labels in train_loader:<br>
inputs, labels = inputs.to(device), labels.to(device)<br>
optimizer.zero_grad()<br>
outputs = model(inputs)<br>
loss = criterion(outputs, labels)<br>
loss.backward()<br>
optimizer.step()<br>
running_loss_train += loss.item()<br>
train_loss = running_loss_train / len(train_loader.dataset)<br>
return train_loss</p>
<p>def validate(model, valid_loader, device, criterion):<br>
“”“Validate model and return the accuracy and mean loss.”“”<br>
model.eval()<br>
correct = 0<br>
running_loss_val = 0<br>
with torch.no_grad():<br>
for inputs, labels in valid_loader:<br>
inputs, labels = inputs.to(device), labels.to(device)<br>
outputs = model(inputs)<br>
loss = criterion(outputs, labels)<br>
pred = outputs.argmax(dim=1)<br>
correct += pred.eq(labels).sum().item()<br>
running_loss_val += loss.item()<br>
val_acc = correct / len(valid_loader.dataset)<br>
val_loss = running_loss_val / len(valid_loader.dataset)<br>
return val_acc, val_loss</p>
<p>def fit(model, train_loader, valid_loader, learning_rate, num_epochs):<br>
criterion = nn.CrossEntropyLoss(reduction=‘sum’)<br>
optimizer = optim.Adam(model.parameters(), lr=learning_rate)<br>
es = EarlyStopping(mode=‘min’, path=‘./EarlyStop.pth’, patience=5)<br>
model = model.to(device)<br>
scheduler = ExponentialLR(optimizer, gamma=0.9)</p>
<pre><code>for epoch in range(1, num_epochs + 1):
    train_loss = train_one_epoch(model, train_loader, optimizer, device, criterion)
    val_acc, val_loss = validate(model, valid_loader, device, criterion)
    scheduler.step()
    print(f'Epoch {epoch:2}/{num_epochs}',
          f'train loss: {train_loss:.4f}',
          f'val loss: {val_loss:.4f}',
          f'val acc: {val_acc:.2%}',
          sep=' | '
         )
    if es(val_loss, model):
        break
</code></pre>
<p>TRAIN_BATCH_SIZE = 64<br>
VALID_BATCH_SIZE = 64<br>
NUM_EPOCHS = 5<br>
LEARNING_RATE = 1e-3<br>
NUM_WORKERS = 0<br>
PIN_MEMORY = True</p>
<p>train_loader = DataLoader(train_dataset, batch_size=TRAIN_BATCH_SIZE, shuffle=False, num_workers=NUM_WORKERS, pin_memory=PIN_MEMORY)<br>
valid_loader = DataLoader(test_dataset, batch_size=VALID_BATCH_SIZE, shuffle=False, num_workers=NUM_WORKERS, pin_memory=PIN_MEMORY)</p>
<p>model = ConvNet()<br>
start = time.time()<br>
fit(model, train_loader, valid_loader, learning_rate=LEARNING_RATE, num_epochs=NUM_EPOCHS)<br>
print(f’Total training time: {time.time() - start}')<br>
model.load_state_dict(torch.load(‘model.pth’))</p>

ptrblck: <p>Based on the error message it seems you are passing an input with 64 channels to the model while a single channel is expected.</p>
<p>Unfortunately, you’ve replaced all code snippet with images, so that I won’t be able to debug it.<br>
Check the shape of <code>x</code> in the <code>forward</code> method and it should show the wrong shape.</p>

Arupreza: <p>I tried to add an early stop but it’s not running, please help me silver this error.</p>
<p>CreatData = []<br>
X = Concatnated[:, :446]<br>
y = Concatnated[:,446]<br>
strat_time = 0<br>
timestamp = 10<br>
length = len(Concatnated)</p>
<p>for i in range(int(abs(length/10-1))):<br>
CreatData.append((X[strat_time:timestamp, :446], y[timestamp]))</p>
<pre><code>strat_time = strat_time + 10
timestamp = timestamp + 10
</code></pre>
<p>train_dataset, test_dataset = train_test_split(CreatData, test_size=0.2)<br>
print("Number of Training Sequences: ", len(train_dataset))<br>
print("Number of Testing Sequences: ", len(test_dataset))</p>
<p>def set_seed(seed=0):<br>
np.random.seed(seed)<br>
random.seed(seed)<br>
torch.manual_seed(seed)<br>
torch.cuda.manual_seed(seed)<br>
torch.cuda.manual_seed_all(seed)<br>
torch.backends.cudnn.deterministic = True<br>
torch.backends.cudnn.benchmark = False<br>
os.environ[‘PYTHONHASHSEED’] = str(seed)</p>
<p>set_seed(0)</p>
<p>class ConvNet(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(ConvNet, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(in_channels = 1, out_channels = 128, kernel_size = 3)<br>
self.pool = nn.MaxPool2d(2,2)<br>
self.conv2 = nn.Conv2d(in_channels = 128, out_channels = 64, kernel_size = 3)</p>
<pre><code>    self.fc1 = nn.Linear(in_features = 64*1*110, out_features = 120)
    self.fc2 = nn.Linear(in_features = 120, out_features = 84)
    self.out = nn.Linear(in_features = 84, out_features = 4)

def forward(self, x):
    x = self.pool(F.relu(self.conv1(x)))  
    x = self.pool(F.relu(self.conv2(x)))  
    x = x.view(-1, 64*1*110)           
    x = F.relu(self.fc1(x))               
    x = F.relu(self.fc2(x))               
    x = self.out(x)                      
    return x
</code></pre>
<p>class EarlyStopping:<br>
def <strong>init</strong>(self, mode, path, patience=3, delta=0):<br>
if mode not in {‘min’, ‘max’}:<br>
raise ValueError(“Argument mode must be one of ‘min’ or ‘max’.”)<br>
if patience &lt;= 0:<br>
raise ValueError(“Argument patience must be a postive integer.”)<br>
if delta &lt; 0:<br>
raise ValueError(“Argument delta must not be a negative number.”)</p>
<pre><code>    self.mode = mode
    self.patience = patience
    self.delta = delta
    self.path = path
    self.best_score = np.inf if mode == 'min' else -np.inf
    self.counter = 0
    
def _is_improvement(self, val_score):
    """Return True iff val_score is better than self.best_score."""
    if self.mode == 'max' and val_score &gt; self.best_score + self.delta:
        return True
    elif self.mode == 'min' and val_score &lt; self.best_score - self.delta:
        return True
    return False
    
def __call__(self, val_score, model):
    """Return True iff self.counter &gt;= self.patience.
    """
    
    if self._is_improvement(val_score):
        self.best_score = val_score
        self.counter = 0
        torch.save(model.state_dict(), self.path)
        print('Val loss improved. Saved model.')
        return False
    else:
        self.counter += 1
        print(f'Early stopping counter: {self.counter}/{self.patience}')
        if self.counter &gt;= self.patience:
            print(f'Stopped early. Best val loss: {self.best_score:.4f}')
            return True
</code></pre>
<p>def train_one_epoch(model, train_loader, optimizer, device, criterion):<br>
“”“Train model for one epoch and return the mean train_loss.”“”<br>
model.train()<br>
running_loss_train = 0<br>
for inputs, labels in train_loader:<br>
inputs, labels = inputs.to(device), labels.to(device)<br>
optimizer.zero_grad()<br>
outputs = model(inputs)<br>
loss = criterion(outputs, labels)<br>
loss.backward()<br>
optimizer.step()<br>
running_loss_train += loss.item()<br>
train_loss = running_loss_train / len(train_loader.dataset)<br>
return train_loss</p>
<p>def validate(model, valid_loader, device, criterion):<br>
“”“Validate model and return the accuracy and mean loss.”“”<br>
model.eval()<br>
correct = 0<br>
running_loss_val = 0<br>
with torch.no_grad():<br>
for inputs, labels in valid_loader:<br>
inputs, labels = inputs.to(device), labels.to(device)<br>
outputs = model(inputs)<br>
loss = criterion(outputs, labels)<br>
pred = outputs.argmax(dim=1)<br>
correct += pred.eq(labels).sum().item()<br>
running_loss_val += loss.item()<br>
val_acc = correct / len(valid_loader.dataset)<br>
val_loss = running_loss_val / len(valid_loader.dataset)<br>
return val_acc, val_loss</p>
<p>def fit(model, train_loader, valid_loader, learning_rate, num_epochs):<br>
criterion = nn.CrossEntropyLoss(reduction=‘sum’)<br>
optimizer = optim.Adam(model.parameters(), lr=learning_rate)<br>
es = EarlyStopping(mode=‘min’, path=‘./EarlyStop.pth’, patience=5)<br>
model = model.to(device)<br>
scheduler = ExponentialLR(optimizer, gamma=0.9)</p>
<pre><code>for epoch in range(1, num_epochs + 1):
    train_loss = train_one_epoch(model, train_loader, optimizer, device, criterion)
    val_acc, val_loss = validate(model, valid_loader, device, criterion)
    scheduler.step()
    print(f'Epoch {epoch:2}/{num_epochs}',
          f'train loss: {train_loss:.4f}',
          f'val loss: {val_loss:.4f}',
          f'val acc: {val_acc:.2%}',
          sep=' | '
         )
    if es(val_loss, model):
        break
</code></pre>
<p>TRAIN_BATCH_SIZE = 64<br>
VALID_BATCH_SIZE = 64<br>
NUM_EPOCHS = 5<br>
LEARNING_RATE = 1e-3<br>
NUM_WORKERS = 0<br>
PIN_MEMORY = True</p>
<p>train_loader = DataLoader(train_dataset, batch_size=TRAIN_BATCH_SIZE, shuffle=False, num_workers=NUM_WORKERS, pin_memory=PIN_MEMORY)<br>
valid_loader = DataLoader(test_dataset, batch_size=VALID_BATCH_SIZE, shuffle=False, num_workers=NUM_WORKERS, pin_memory=PIN_MEMORY)</p>
<p>model = ConvNet()<br>
start = time.time()<br>
fit(model, train_loader, valid_loader, learning_rate=LEARNING_RATE, num_epochs=NUM_EPOCHS)<br>
print(f’Total training time: {time.time() - start}')<br>
model.load_state_dict(torch.load(‘model.pth’))</p>

ptrblck: <p>Your code is still not formatted correctly (you can wrap it into three backticks ```) and is not executable, as objects are undefined.<br>
Your model generally works for a proper input shape:</p>
<pre><code class="lang-python">class ConvNet(nn.Module):
    def __init__(self):
        super(ConvNet, self).__init__()
        self.conv1 = nn.Conv2d(in_channels = 1, out_channels = 128, kernel_size = 3)
        self.pool = nn.MaxPool2d(2,2)
        self.conv2 = nn.Conv2d(in_channels = 128, out_channels = 64, kernel_size = 3)
        
        self.fc1 = nn.Linear(in_features = 2304, out_features = 120)
        self.fc2 = nn.Linear(in_features = 120, out_features = 84)
        self.out = nn.Linear(in_features = 84, out_features = 4)
    
    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))  
        x = self.pool(F.relu(self.conv2(x)))  
        x = x.view(x.size(0), -1) 
        x = F.relu(self.fc1(x))               
        x = F.relu(self.fc2(x))               
        x = self.out(x)                      
        return x


model = ConvNet()
x = torch.randn(2, 1, 32, 32)
out = model(x)
</code></pre>

Arupreza: <p>Thanks for your great help it will inspire me to stay connected in pytorch.</p>
<pre><code class="lang-auto">#Data Pre processing
CreatData = []
X = Concatnated[:, :446]
y = Concatnated[:,446]
strat_time = 0
timestamp = 10
length = len(Concatnated)

for i in range(int(abs(length/10-1))):
    CreatData.append((X[strat_time:timestamp, :446], y[timestamp]))

    strat_time = strat_time + 10
    timestamp = timestamp + 10

#Data Splitting
train_dataset, test_dataset = train_test_split(CreatData, test_size=0.2)
print("Number of Training Sequences: ", len(train_dataset))
print("Number of Testing Sequences: ", len(test_dataset))

#Asign Seeds
def set_seed(seed=0):
    np.random.seed(seed)
    random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    os.environ['PYTHONHASHSEED'] = str(seed)
    
set_seed(0)

#Architechture
class ConvNet(nn.Module):
    def __init__(self):
        super(ConvNet, self).__init__()
        self.conv1 = nn.Conv2d(in_channels = 1, out_channels = 128, kernel_size = 3)
        self.pool = nn.MaxPool2d(2,2)
        self.conv2 = nn.Conv2d(in_channels = 128, out_channels = 64, kernel_size = 3)
        
        self.fc1 = nn.Linear(in_features = 64*1*110, out_features = 120)
        self.fc2 = nn.Linear(in_features = 120, out_features = 84)
        self.out = nn.Linear(in_features = 84, out_features = 4)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))  
        x = self.pool(F.relu(self.conv2(x)))  
        x = x.view(-1, 64*1*110)           
        x = F.relu(self.fc1(x))               
        x = F.relu(self.fc2(x))               
        x = self.out(x)                      
        return x

#Early Stop
class EarlyStopping:
    def __init__(self, mode, path, patience=3, delta=0):
        if mode not in {'min', 'max'}:
            raise ValueError("Argument mode must be one of 'min' or 'max'.")
        if patience &lt;= 0:
            raise ValueError("Argument patience must be a postive integer.")
        if delta &lt; 0:
            raise ValueError("Argument delta must not be a negative number.")
            
        self.mode = mode
        self.patience = patience
        self.delta = delta
        self.path = path
        self.best_score = np.inf if mode == 'min' else -np.inf
        self.counter = 0
        
    def _is_improvement(self, val_score):
        """Return True iff val_score is better than self.best_score."""
        if self.mode == 'max' and val_score &gt; self.best_score + self.delta:
            return True
        elif self.mode == 'min' and val_score &lt; self.best_score - self.delta:
            return True
        return False
        
    def __call__(self, val_score, model):
        """Return True iff self.counter &gt;= self.patience.
        """
        
        if self._is_improvement(val_score):
            self.best_score = val_score
            self.counter = 0
            torch.save(model.state_dict(), self.path)
            print('Val loss improved. Saved model.')
            return False
        else:
            self.counter += 1
            print(f'Early stopping counter: {self.counter}/{self.patience}')
            if self.counter &gt;= self.patience:
                print(f'Stopped early. Best val loss: {self.best_score:.4f}')
                return True

#Train Set
def train_one_epoch(model, train_loader, optimizer, device, criterion):
    """Train model for one epoch and return the mean train_loss."""
    model.train()
    running_loss_train = 0
    for inputs, labels in train_loader:
        inputs, labels = inputs.to(device), labels.to(device)
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss_train += loss.item()
    train_loss = running_loss_train / len(train_loader.dataset)
    return train_loss

#Validation Set
def validate(model, valid_loader, device, criterion):
    """Validate model and return the accuracy and mean loss."""
    model.eval()
    correct = 0
    running_loss_val = 0
    with torch.no_grad():
        for inputs, labels in valid_loader:
            inputs, labels = inputs.to(device), labels.to(device)
            outputs = model(inputs)
            loss = criterion(outputs, labels)
            pred = outputs.argmax(dim=1)
            correct += pred.eq(labels).sum().item()
            running_loss_val += loss.item()
    val_acc = correct / len(valid_loader.dataset)
    val_loss = running_loss_val / len(valid_loader.dataset)
    return val_acc, val_loss

#Train model
def fit(model, train_loader, valid_loader, learning_rate, num_epochs):
    criterion = nn.CrossEntropyLoss(reduction='sum')
    optimizer = optim.Adam(model.parameters(), lr=learning_rate)
    es = EarlyStopping(mode='min', path='./EarlyStop.pth', patience=5)
    model = model.to(device)
    scheduler = ExponentialLR(optimizer, gamma=0.9)

    for epoch in range(1, num_epochs + 1):
        train_loss = train_one_epoch(model, train_loader, optimizer, device, criterion)
        val_acc, val_loss = validate(model, valid_loader, device, criterion)
        scheduler.step()
        print(f'Epoch {epoch:2}/{num_epochs}',
              f'train loss: {train_loss:.4f}',
              f'val loss: {val_loss:.4f}',
              f'val acc: {val_acc:.2%}',
              sep=' | '
             )
        if es(val_loss, model):
            break

#Run the model
TRAIN_BATCH_SIZE = 64
VALID_BATCH_SIZE = 64
NUM_EPOCHS = 5
LEARNING_RATE = 1e-3
NUM_WORKERS = 0
PIN_MEMORY = True

train_loader = DataLoader(train_dataset, batch_size=TRAIN_BATCH_SIZE, shuffle=False, num_workers=NUM_WORKERS, pin_memory=PIN_MEMORY)
valid_loader = DataLoader(test_dataset, batch_size=VALID_BATCH_SIZE, shuffle=False, num_workers=NUM_WORKERS, pin_memory=PIN_MEMORY)


model = ConvNet()
start = time.time()
fit(model, train_loader, valid_loader, learning_rate=LEARNING_RATE, num_epochs=NUM_EPOCHS)
print(f'Total training time: {time.time() - start}')
model.load_state_dict(torch.load('EarlyStop.pth'))

</code></pre>

Arupreza: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> I am stuck on this point but as far as I found my architecture is ok, can you please give me some suggestions?</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/4/341886696f5526ec68b013678b2ab654101cca9b.png" alt="image" data-base62-sha1="7qRn8jYEeXiDpc9xM48SO7vwZBx" width="489" height="254"></p>

ptrblck: <p>Where exactly are you stuck?<br>
My code snippet should work fine and your posted code is still not executable since undefined objects are used.<br>
Use my code snippet as a template, compare it to your code, and try to narrow down where the differences are coming from.</p>

Arupreza: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/9/a991635685266ddebce091e6d2241cfb6d5676f4.png" data-download-href="https://discuss.pytorch.org/uploads/default/a991635685266ddebce091e6d2241cfb6d5676f4" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/9/a991635685266ddebce091e6d2241cfb6d5676f4.png" alt="image" data-base62-sha1="oc4bsV20royJQ8ANBJWB7xEwyMI" width="490" height="500" data-dominant-color="F4F5F5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">803×819 57.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p><code>inputs</code> is in this case still wrong and you would either need to change the <code>in_channels</code> of the first layer to <code>64</code> or fix the input to use a single channel.</p>

Arupreza: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Thanks a lot its worked</p>