The Wayback Machine - https://web.archive.org/web/20241123012804/https://www.geeksforgeeks.org/program-print-diamond-shape/
Open In App

Program to print the Diamond Shape

Last Updated : 11 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a number n, write a program to print a diamond shape with 2n rows.

Examples : 

diamond

C++
// C++ program to print diamond shape
// with 2n rows 
#include <bits/stdc++.h>
using namespace std;

// Prints diamond pattern with 2n rows 
void printDiamond(int n) 
{ 
    int space = n - 1; 

    // run loop (parent loop) 
    // till number of rows 
    for (int i = 0; i < n; i++) 
    { 
        // loop for initially space, 
        // before star printing 
        for (int j = 0;j < space; j++) 
            cout << " "; 

        // Print i+1 stars 
        for (int j = 0; j <= i; j++) 
            cout << "* "; 

        cout << endl; 
        space--; 
    } 

    // Repeat again in reverse order 
    space = 0; 

    // run loop (parent loop) 
    // till number of rows 
    for (int i = n; i > 0; i--) 
    { 
        // loop for initially space, 
        // before star printing 
        for (int j = 0; j < space; j++) 
            cout << " "; 

        // Print i stars 
        for (int j = 0;j < i;j++) 
            cout << "* ";

        cout << endl;
        space++; 
    } 
} 

// Driver code 
int main() 
{ 
    printDiamond(5); 
    return 0; 
} 

// This is code is contributed
// by rathbhupendra
C
// C program to print 
// diamond shape with 
// 2n rows
#include<stdio.h>

// Prints diamond 
// pattern with 2n rows
void printDiamond(int n)
{
    int space = n - 1;

    // run loop (parent loop)
    // till number of rows
    for (int i = 0; i < n; i++)
    {
        // loop for initially space, 
        // before star printing
        for (int j = 0;j < space; j++)
            printf(" ");

        // Print i+1 stars
        for (int j = 0;j <= i; j++)
            printf("* ");

        printf("\n");
        space--;
    }

    // Repeat again in 
    // reverse order
    space = 0;

    // run loop (parent loop)
    // till number of rows
    for (int i = n; i > 0; i--)
    {
        // loop for initially space, 
        // before star printing
        for (int j = 0; j < space; j++)
            printf(" ");

        // Print i stars
        for (int j = 0;j < i;j++)
            printf("* ");

        printf("\n");
        space++;
    }
}

// Driver code
int main()
{
    printDiamond(5);
    return 0;
}
Java
// JAVA Code to print 
// the diamond shape
import java.util.*;

class GFG
{
    
    // Prints diamond pattern
    // with 2n rows
    static void printDiamond(int n)
    {
        int space = n - 1;
    
        // run loop (parent loop) 
        // till number of rows
        for (int i = 0; i < n; i++)
        {
            // loop for initially space, 
            // before star printing
            for (int j = 0; j < space; j++)
                System.out.print(" ");
    
            // Print i+1 stars
            for (int j = 0; j <= i; j++)
                System.out.print("* ");
    
            System.out.print("\n");
            space--;
        }
    
        // Repeat again in
        // reverse order
        space = 0;
    
        // run loop (parent loop) 
        // till number of rows
        for (int i = n; i > 0; i--)
        {
            // loop for initially space, 
            // before star printing
            for (int j = 0; j < space; j++)
                System.out.print(" ");
    
            // Print i stars
            for (int j = 0; j < i; j++)
                System.out.print("* ");
    
            System.out.print("\n");
            space++;
        }
    }
    
    // Driver Code
    public static void main(String[] args) 
    {
        printDiamond(5);
        
    }
}

// This code is contributed
// by Arnav Kr. Mandal. 
Python
# Python program to
# print Diamond shape

# Function to print
# Diamond shape
def Diamond(rows):
    n = 1
    for i in range(1, rows + 1):
        # loop to print spaces
        for j in range (1, (rows - i) + 1):
            print(end = " ")
        
        # loop to print star
        while n != (i+1):
            print("*", end = " ")
            n = n + 1
        n = 1
        
        # line break
        print()

    k = 0
    n = 0
    for i in range(1, rows + 1):
        # loop to print spaces
        for j in range (1, k + 1):
            print(end = " ")
        k = k + 1
        
        # loop to print star
        while n <= (rows - i):
            print("*", end = " ")
            n = n + 1
        n = 0
        print()

# Driver Code
# number of rows input
rows = 5
Diamond(rows)
C#
// C# Code to print 
// the diamond shape
using System;

class GFG 
{
    
    // Prints diamond pattern
    // with 2n rows
    static void printDiamond(int n)
    {
        int space = n - 1;
    
        // run loop (parent loop) 
        // till number of rows
        for (int i = 0; i < n; i++)
        {
            // loop for initially space,
            // before star printing
            for (int j = 0; j < space; j++)
                Console.Write(" ");
    
            // Print i+1 stars
            for (int j = 0; j <= i; j++)
                Console.Write("* ");
    
            Console.Write("\n");
            space--;
        }
    
        // Repeat again in
        // reverse order
        space = 0;
    
        // run loop (parent loop)
        // till number of rows
        for (int i = n; i > 0; i--)
        {
            // loop for initially space, 
            // before star printing
            for (int j = 0; j < space; j++)
                Console.Write(" ");
    
            // Print i stars
            for (int j = 0; j < i; j++)
                Console.Write("* ");
    
            Console.Write("\n");
            space++;
        }
    }
    
    // Driver Code
    public static void Main() 
    {
        printDiamond(5);
        
    }
}

// This code is contributed 
// by Smitha Semwal. 
JavaScript
<script>
      // JavaScript program to print diamond shape
      // with 2n rows

      // Prints diamond pattern with 2n rows
      function printDiamond(n) {
        var space = n - 1;

        // run loop (parent loop)
        // till number of rows
        for (var i = 0; i < n; i++) {
          // loop for initially space,
          // before star printing
          for (var j = 0; j < space; j++) document.write("&nbsp;&nbsp;");

          // Print i+1 stars
          for (var j = 0; j <= i; j++) document.write("*" + "&nbsp;&nbsp;");

          document.write("<br>");
          space--;
        }

        // Repeat again in reverse order
        space = 0;

        // run loop (parent loop)
        // till number of rows
        for (var i = n; i > 0; i--) 
        {
        
          // loop for initially space,
          // before star printing
          for (var j = 0; j < space; j++) document.write("&nbsp;&nbsp;");

          // Print i stars
          for (var j = 0; j < i; j++) document.write("*" + "&nbsp;&nbsp;");

          document.write("<br>");
          space++;
        }
      }

      // Driver code
      printDiamond(5);
      
      // This code is contributed by rdtank.
    </script>
PHP
<?php
// PHP program to print 
// diamond shape with 
// 2n rows

// Prints diamond $
// pattern with 2n rows
function printDiamond($n)
{
    $space = $n - 1;

    // run loop (parent loop)
    // till number of rows
    for ($i = 0; $i < $n; $i++)
    {
        
        // loop for initially space, 
        // before star printing
        for ($j = 0;$j < $space; $j++)
            printf(" ");

        // Print i+1 stars
        for ($j = 0;$j <= $i; $j++)
            printf("* ");

        printf("\n");
        $space--;
    }

    // Repeat again in 
    // reverse order
    $space = 0;

    // run loop (parent loop)
    // till number of rows
    for ($i = $n; $i > 0; $i--)
    {
        
        // loop for initially space, 
        // before star printing
        for ($j = 0; $j < $space; $j++)
            printf(" ");

        // Pr$i stars
        for ($j = 0;$j < $i;$j++)
            printf("* ");

        printf("\n");
        $space++;
    }
}

    // Driver code
    printDiamond(5);

// This code is contributed by Anuj_67
?>

Output
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 

Time Complexity: O(n*n) since we are traversing rows and columns of a grid for printing spaces ‘ ‘ and star ‘*’.
Auxiliary Space: O(1), No extra Space used.

For more practice with pattern printing and mastering loops in C, our C programming course provides a variety of exercises and projects to strengthen your understanding of these core concepts.

Approach 2: Solving the problem using Recursion

Below is the implementation of the above approach:

C++
// C++ program to print diamond pattern using recursion
#include <bits/stdc++.h>
using namespace std;
void gotonextLine(int k, int i, int z)
{
    if (k == i) // base case
        return;
    cout << "* ";
    gotonextLine(k + z, i, z);
}
void addblankSpaceInDiamond(
    int j, int i, int z) // print blank space of diamond
{
    if (j == i)
        return;
    cout << " ";
    addblankSpaceInDiamond(j + z, i, z);
}
void upperDiamond(int row, int i)
{
    if (i > row) // base case
        return;
    addblankSpaceInDiamond(row, i, -1);
    gotonextLine(0, i, 1);
    cout << endl;
    upperDiamond(row, i + 1); // recursive call
}
void lowerDiamond(int row,
                  int i) // print the next line of diamond
{
    if (i > row) // base case
        return;
    addblankSpaceInDiamond(0, i, 1);
    gotonextLine(row, i, -1);
    cout << endl;
    lowerDiamond(row, i + 1);
}
int main()
{
    int row;
    row = 5;
    upperDiamond(row, 0); // print upper part of triangle
    lowerDiamond(row, 1); // print lower part of diamond
    return 0;
    // this code is contributed by Shivesh Kumar Dwivedi
}
Java
// Java program to print diamond pattern using recursion
import java.io.*;
 
class GFG{
     
static void gotonextLine(int k, int i, int z)
{
    if (k == i) // base case
        return;
    System.out.print("* ");
    gotonextLine(k + z, i, z);
}
static void addblankSpaceInDiamond(int j, int i, int z) // print blank space of diamond
{
    if (j == i)
        return;
    System.out.print(" ");
    addblankSpaceInDiamond(j + z, i, z);
} 
static void upperDiamond(int row, int i)
{
    if (i > row) // base case
        return;
    addblankSpaceInDiamond(row, i, -1);
    gotonextLine(0, i, 1);
    System.out.print("\n");
    upperDiamond(row, i + 1); // recursive call
}
static void lowerDiamond(int row, int i) // print the next line of diamond
{
    if (i > row) // base case
        return;
    addblankSpaceInDiamond(0, i, 1);
    gotonextLine(row, i, -1);
    System.out.print("\n");
    lowerDiamond(row, i + 1);
}
 
// Driver Code
public static void main(String[] args)
{
    int row;
    row = 5;
    upperDiamond(row, 0); // print upper part of triangle
    lowerDiamond(row, 1); // print lower part of diamond
}
}
 
// This code is contributed by adityapatil12
Python
def gotonextLine(k, i, z):
  # base case
    if (k == i):
      return
    print("* ", end=""),
    gotonextLine(k + z, i, z)

# print blank space of diamond
def addblankSpaceInDiamond(j,i,z):
    if (j == i):
      return
    print(" ",end=""),
    addblankSpaceInDiamond(j + z, i, z)

def upperDiamond(row,i):
  # base case
    if (i > row):
      return
    addblankSpaceInDiamond(row, i, -1)
    gotonextLine(0, i, 1)
    print("\n",end=""),
    upperDiamond(row, i + 1) # recursive call


def lowerDiamond(row,i):
  # print the next line of diamond
    if (i > row): # base case
      return
    addblankSpaceInDiamond(0, i, 1)
    gotonextLine(row, i, -1)
    print("\n",end=""),
    lowerDiamond(row, i + 1)

# Code
row = 5
upperDiamond(row, 0) # print upper part of triangle
lowerDiamond(row, 1) # print lower part of diamond

# This code is contributed by akashish__
C#
using System;

public class GFG{

  public static void gotonextLine(int k, int i, int z)
  {
    if (k == i) // base case
      return;
    Console.Write("* ");
    gotonextLine(k + z, i, z);
  }

  public static void addblankSpaceInDiamond(
    int j, int i, int z) // print blank space of diamond
  {
    if (j == i)
      return;
    Console.Write(" ");
    addblankSpaceInDiamond(j + z, i, z);
  }

  public static void upperDiamond(int row, int i)
  {
    if (i > row) // base case
      return;
    addblankSpaceInDiamond(row, i, -1);
    gotonextLine(0, i, 1);
    Console.Write("\n");
    upperDiamond(row, i + 1); // recursive call
  }


  public static void lowerDiamond(int row,
                                  int i) // print the next line of diamond
  {
    if (i > row) // base case
      return;
    addblankSpaceInDiamond(0, i, 1);
    gotonextLine(row, i, -1);
    Console.Write("\n");
    lowerDiamond(row, i + 1);
  }

  public static void Main ()
  {

    // Code
    int row;
    row = 5;
    upperDiamond(row, 0); // print upper part of triangle
    lowerDiamond(row, 1); // print lower part of diamond
  }
}

// This code is contributed by akashish__
JavaScript
    // JavaScript program to print diamond pattern using recursion         
    function gotonextLine(k, i, z)
    {
        if (k == i) // base case
            return;
        console.log("*&nbsp;");
        gotonextLine(k + z, i, z);
    }
    function addblankSpaceInDiamond(j, i, z) // print blank space of diamond
    {
        if (j == i)
            return;
        console.log("&nbsp;");
        addblankSpaceInDiamond(j + z, i, z);
    } 
    function upperDiamond(row, i)
    {
        if (i > row) // base case
            return;
        addblankSpaceInDiamond(row, i, -1);
        gotonextLine(0, i, 1);
        console.log("<br>");
        upperDiamond(row, i + 1); // recursive call
    }
    function lowerDiamond(row, i) // print the next line of diamond
    {
        if (i > row) // base case
            return;
        addblankSpaceInDiamond(0, i, 1);
        gotonextLine(row, i, -1);
        console.log("<br>");
        lowerDiamond(row, i + 1);
    }
     
    // Driver Code 
        let row;
        row = 5;
        upperDiamond(row, 0); // print upper part of triangle
        lowerDiamond(row, 1); // print lower part of diamond
    
    // This code is contributed by agfro1cac

Output
     
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 
     

Time Complexity: O(N2), Since we are traversing rows and columns of a grid for printing spaces ‘ ‘ and star ‘*’.
Auxiliary Space: O(N), The extra space is used in recursion call stack.



Previous Article
Next Article

Similar Reads

Draw Diamond shape using Turtle graphics in Python
In this article, we are going to learn how to draw the shape of a Diamond using turtle graphics in Python. Turtle graphics: forward(length): moves the pen in the forward direction by x unit.right(angle): rotate the pen in the clockwise direction by an angle x.left(angle): rotate the pen in the anticlockwise direction by an angle x. Approach: Import
2 min read
Draw Shape inside Shape in Python Using Turtle
Prerequisites: Turtle Programming in Python Turtle is a Python feature like a drawing board, which let us command a turtle to draw all over it! We can use many turtle functions which can move the turtle around. Turtle comes in the turtle library. The turtle module can be used in both object-oriented and procedure-oriented ways. Some of the commonly
3 min read
Program to print half Diamond star pattern
Given an integer N, the task is to print half-diamond-star pattern. ************************************ Examples: Input: N = 3 Output: * ** *** ** * Input: N = 6 Output: * ** *** **** ***** ****** ***** **** *** ** * Approach: The idea is to break the pattern into two halves that is upper half and lower half. Then print them separately with the he
4 min read
Program to print diamond pattern using numbers and stars
Program to print the following pattern of a half diamond for N. Example: Input: N = 5Output: 1 2*3 4*5*6 7*8*9*10 11*12*13*14*15 11*12*13*14*15 7*8*9*10 4*5*6 2*3 1 This program is divided into four parts. C/C++ Code // C++ program to print the half diamond // pattern using numbers and stars #include <iostream> using namespace std; void patte
5 min read
Program to print hollow pyramid, diamond pattern and their modifications
For Prerequisite : Loops, If Else Statement1. Hollow pyramid/triangle pattern The pattern is similar to pyramid pattern. The only difference is, we will replace all internal '#' or '*' characters by space character and we will print 2*N-1 (N = number of rows in pattern) '#' or '*' characters in last row. Examples: Input: n=6 Output: # # # # # # # #
15+ min read
Programs to print Triangle and Diamond patterns using recursion
This article is aimed at giving a recursive implementation for pattern printing. Pattern 1: Example:Input: 5 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Implementation: C/C++ Code // Program to print the given pattern #include <iostream> using namespace std; void print_asterisk(int asterisk) { if (asterisk == 0) return;
15+ min read
Program for diamond pattern with different layers
Given a number n and using 0-n numbers you have to print such a pattern.Examples: Input : n = 5 Output : 0 0 1 0 0 1 2 1 0 0 1 2 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 4 5 4 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 2 1 0 0 1 2 1 0 0 1 0 0 Input : n = 3 Output : 0 0 1 0 0 1 2 1 0 0 1 2 3 2 1 0 0 1 2 1 0 0 1 0 0 The idea here is to count the space in beginning o
10 min read
Diamond operator for Anonymous Inner Class with Examples in Java
Prerequisite: Anonymous Inner Class Diamond Operator: Diamond operator was introduced in Java 7 as a new feature.The main purpose of the diamond operator is to simplify the use of generics when creating an object. It avoids unchecked warnings in a program and makes the program more readable. The diamond operator could not be used with Anonymous inn
2 min read
Diamond Tree
Given a number K, the task is to create the Diamond-like structure of Tree which follows these two conditions: First K levels of the tree should be a balanced binary tree and the value of nodes should start from 1 and increases from left to right.The next K - 1 levels should form a diamond-like structure by merging every pair of nodes with their su
14 min read
bokeh.plotting.figure.diamond() function in Python
Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like notebook, HTML and server. Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with default axes, grids, tools, etc. bokeh.plotting.figur
2 min read
Simple Diamond Pattern in Python
Given an integer n, the task is to write a python program to print diamond using loops and mathematical formulations. The minimum value of n should be greater than 4. Examples : For size = 5 * * * * * * * * * * * * For size = 8 * * * * * * * * * * * * * * * * * * * * * * * * * * For size = 11 * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3 min read
Find the row up to which there are at least K stars in the Diamond Pattern
Given two integers N and K, where N represents a diamond pattern with (2 * N) -1 rows, the task is to find the index of the first row up to which there are at least K stars in a diamond pattern. Please note that the value of K will always have a definite answer. Examples: Input: N = 3 , K = 8Output: 4Explanation: The first 4 rows contain a total of
11 min read
Create a prefect diamond from the elements of the given String
Given a string str, the task is to print the perfect diamond from the elements of the given string. Examples: Input: str = " geeksforgeeks"Output: g e e k s f o r g eliminate 'eeks' because it is not required to make a perfect diamond from the given string Input: str = "good"Output: g o o d Approach: This can be solved with the following idea: We n
7 min read
Diamond Problem in C++
C++ is one of the most powerful object-oriented programming language. However, with great power comes great responsibility, especially when dealing with complex inheritance structures. One such challenge is the Diamond Problem, which can arise when using multiple inheritance in C++ leading unexpected behaviour making code difficult-to-debug. In thi
4 min read
Draw a rectangular shape and extract objects using Python's OpenCV
OpenCV is an open-source computer vision and machine learning software library. Various image processing operations such as manipulating images and applying tons of filters can be done with the help of it. It is broadly used in Object detection, Face Detection, and other Image processing tasks. Let's see how to draw rectangular shape on image and e
4 min read
Find the maximum sum of Plus shape pattern in a 2-D array
Given a 2-D array of size N*M where, [Tex]3\leq N, M \leq 1000 [/Tex]. The task is to find the maximum value achievable by a + shaped pattern. The elements of the array can be negative.The plus(+) shape pattern is formed by taking any element with co-ordinate (x, y) as a center and then expanding it in all four directions(if possible). A plus(+) sh
10 min read
Pandas df.size, df.shape and df.ndim Methods
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas size, shape, and ndim methods are used to return the size, shape, and dimensions of data frames and series. Dataset Used To downl
3 min read
Check if the given 2-D points form T-shape or not
Given the coordinates of 5 2-dimensional points, check if they form a closed T shape. Print 'Yes' if they form T shape and 'No' otherwise. Note: Coordinates should be distinct and integers.There are 4-types of T shaped formations possible according to the given conditions: Examples: Input: [[7, 5], [8, 5], [6, 5], [7, 7], [7, 6]] Output: Yes Input:
12 min read
Traverse matrix in L shape
Given a N * M matrix. The task is to traverse the given matrix in L shape as shown in below image. Examples: Input : n = 3, m = 3 a[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } } Output : 1 4 7 8 9 2 5 6 3 Input : n = 3, m = 4 a[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12} } Output : 1 4 7 10 11 12 2 5 8 9 3 6 Observe there will be
5 min read
Python | Pandas TimedeltaIndex.shape
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.shape attribute return a tuple of the shape of the underlying data present in the object under consideration. Synt
2 min read
Python | Pandas Series.shape
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object suppo
2 min read
Python | Pandas Panel.shape
In Pandas, Panel is a very important container for three-dimensional data. The names for the 3 axes are intended to give some semantic meaning to describing operations involving panel data and, in particular, econometric analysis of panel data.In Pandas Panel.shape can be used to get a tuple of axis dimensions. Syntax: Panel.shapeParameters: NoneRe
2 min read
Python | Pandas Index.shape
Pandas Index is an immutable ndarray implementing an ordered, sliceable set. It is the basic object which stores the axis labels for all pandas objects. Pandas Index.shape attribute return a tuple of the shape of the underlying data in the given Index object. Syntax: Index.shape Parameter : None Returns : tuple Example #1: Use Index.shape attribute
2 min read
PyQt5 - Tube shape Radio button
In this article we will see how to create a tube shaped radio button. By default the shape of radio button is rectangle. Below is the representation of normal bordered radio button vs the tube shaped radio button. In order to do this we have to do the following: 1. Create a radio button 2. Set width greater than the height of radio button 3. Add bo
2 min read
PyQt5 QCalendarWidget - Changing Cursor Shape
In this article we will see how we can change the cursor shape for the QCalendarWidget. Cursor is the mouse pointer by default cursor shape i.e cursor look same for both window or calendar although there are a variety of cursor available for calendar, for example, split cursor, size cursor etc. In order to do this we will use setCursor method with
1 min read
PyQt5 QCalendarWidget - Getting Cursor Shape
In this article we will see how we can get the cursor shape of the QCalendarWidget. Cursor is the mouse pointer by default cursor shape i.e cursor look same for both window or calendar although there are a variety of cursor available for calendar, for example, split cursor, size cursor etc. By default, it has the arrow cursor but can be changed wit
2 min read
PyQt5 QCalendarWidget - Making Cursor Shape back to normal
In this article we will see how we can reset the cursor shape of the QCalendarWidget. Cursor is the mouse pointer by default cursor shape i.e cursor look same for both window or calendar although there are a variety of cursor available for calendar, for example, split cursor, size cursor etc. By default, it has the arrow cursor but can be changed w
2 min read
turtle.shape() function in Python
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.shape() This function is used to set the turtle shape to shape with a given name or, if the name is not given, return the name
1 min read
Python - tensorflow.IndexedSlices.shape Attribute
TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks. shape is used to get the tensorflow.TensorShape representing the shape of the dense tensor. Syntax: tensorflow.IndexedSlices.shape Returns: It returns tensorflow.TensorShape representing the shape of the dense tensor. Ex
1 min read
TensorFlow - How to create a tensor of all ones that has the same shape as the input tensor
TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks. Method Used: ones_like: This method accepts a Tensor as input and returns a Tensor with same shape having all values set to one. Example 1: C/C++ Code # importing the library import tensorflow as tf # Initializing the In
1 min read
three90RightbarBannerImg